简体   繁体   English

以编程方式配置NLog

[英]Programmatically configure NLog

i have these four parameters that return nothing to database : Why this is happening and how to solve it ? 我有这四个参数,它们对数据库什么也不返回:为什么会发生这种情况以及如何解决呢?

This is the full code : 这是完整的代码:

https://www.codepile.net/pile/z1lol0Gb https://www.codepile.net/pile/z1lol0Gb

Database is here : 数据库在这里:

https://www.codepile.net/pile/01JgbjXV https://www.codepile.net/pile/01JgbjXV

Part of my code 我的代码的一部分

var dbTarget = new DatabaseTarget ();
dbTarget.ConnectionString = @"data source=.;initial catalog=Database5;user id=App;password=App;";
dbTarget.CommandText =
    @"insert into Logs (Level,CallSite,Type,Message,StackTrace,InnerException,AdditionalInfo) values (
         @level,
         @callSite,
         @type,
         @message,
         @stackTrace,
         @innerException,
         @additionalInfo
         );";
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@level", new NLog.Layouts.SimpleLayout ("${level}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@callSite", new NLog.Layouts.SimpleLayout ("${callsite}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@type", new NLog.Layouts.SimpleLayout ("${exception:format=type}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@message", new NLog.Layouts.SimpleLayout ("${exception:format=message}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@stackTrace", new NLog.Layouts.SimpleLayout ("${${exception:format=stackTrace}}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@innerException", new NLog.Layouts.SimpleLayout ("${exception:format=:innerFormat=ShortType,Message,Method:MaxInnerExceptionLevel=1:InnerExceptionSeparator}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@additionalInfo", new NLog.Layouts.SimpleLayout ("${message}")));

I have the same database that exist here : 我这里有相同的数据库:

https://knightcodes.com/.net/2016/05/25/logging-to-a-database-wth-nlog.html https://knightcodes.com/.net/2016/05/25/logging-to-a-database-wth-nlog.html

This how they look in database 这是他们在数据库中的外观

在此处输入图片说明

Database should look like this : 数据库应如下所示: 在此处输入图片说明

Inner exception can be empty so you can have empty values in your DB (but you must verify if it will save if inner exception is present). 内部异常可以为空,因此您的数据库中可以有空值(但是,如果存在内部异常,则必须验证它是否将保存)。 As for other properties I don't think someone can help you with sample you provided. 至于其他属性,我认为没有人可以帮助您提供您提供的样品。 Problem seems to lay in exception handling and using logger. 问题似乎在于异常处理和使用记录器。 However I can't verify this with your sample. 但是,我无法通过您的示例进行验证。 Here is problem that seems similar to yours. 是一个看起来与您相似的问题。 You can also read about layour renderer and look for answers on NLog wiki. 您还可以阅读有关layour渲染器的信息,并在NLog Wiki上寻找答案。 If you are not already doing this consider using NLog config file to have everything concerning configuration in one place. 如果尚未执行此操作,请考虑使用NLog配置文件将与配置有关的所有内容都放在一个位置。

You need to proper log your exception. 您需要正确记录您的异常。

You code shows: 您的代码显示:

Logger logger1 = LogManager.GetLogger ("Logger1");
// Log something..
logger1.Error("Test Logger1");

but the Exception should be the first argument, see How to log exceptions in NLog 但是Exception应该是第一个参数,请参阅如何在NLog中记录异常

Logger logger1 = LogManager.GetLogger ("Logger1");
// Log something..
logger1.Error(new Exception("my error"), "Test Logger1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM