[英]Add database parameters to NLog programmatically
I want to log message to my database by NLog. 我想通过NLog将消息记录到我的数据库。 Suppose I have the database ready. 假设我准备好了数据库。
var dbTarget = new DatabaseTarget();
dbTarget.Name = "test";
dbTarget.ConnectionString = loggerModel.connection_string;
dbTarget.CommandText = "insert into NlogLogTable(LogDate,LogLevel,LogLogger,LogMessage,LogMachineName,LogUserName,LogCallSite,LogThreadId,LogThreadName,LogException,LogStackTrace) values(@LogDate,@LogLevel,@LogLogger,@LogMessage,@LogMachineName,@LogUserName,@LogCallSite,@LogThreadId,@LogThreadName,@LogException,@LogStackTrace);";
var dateTime = DateTime.Now.ToString();
var dateTimeOffset = DateTimeOffset.Now.ToString();
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogDate", new NLog.Layouts.SimpleLayout("${LogDate}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogLevel", new NLog.Layouts.SimpleLayout("${LogLevel}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogLogger", new NLog.Layouts.SimpleLayout("${LogLogger}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogMessage", new NLog.Layouts.SimpleLayout("${LogMessage}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogMachineName", new NLog.Layouts.SimpleLayout("${LogMachineName}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogUserName", new NLog.Layouts.SimpleLayout("${LogUserName}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogCallSite", new NLog.Layouts.SimpleLayout("${LogCallSite}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogThreadId", new NLog.Layouts.SimpleLayout("${LogThreadId}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogThreadName", new NLog.Layouts.SimpleLayout("${LogThreadName}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogException", new NLog.Layouts.SimpleLayout("${LogException}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogStackTrace", new NLog.Layouts.SimpleLayout("${LogStackTrace}")));
The thing is that I don't know how to pass the parameters to it. 问题是我不知道如何将参数传递给它。 Let's say LogDate
is today's datetime. 假设LogDate
是今天的日期时间。 Also I have command timeout (milliseconds) value, I am not sure how to pass it as well. 我也有命令超时 (毫秒)值,我不知道如何传递它。
There are various options to pass LogDate
and other custom values. 有多种选项可以传递LogDate
和其他自定义值。
${event-properties:LogDate}
and add the properties to your logEvent. 使用${event-properties:LogDate}
并将属性添加到logEvent。 (see EventProperties-Layout-Renderer . (请参阅EventProperties-Layout-Renderer 。 ${date}
layout renderer, like ${date:format=yyyyMMdd}
使用${date}
布局渲染器,例如${date:format=yyyyMMdd}
${mdc:item=String}
or ${gdc:item=String}
使用${mdc:item=String}
或${gdc:item=String}
For the command timeout you can use 2. or 3. 对于命令超时,您可以使用2.或3。
All possible values are listed on the NLog wiki . 所有可能的值都列在NLog wiki上 。
edit: the code for command timeout 编辑: 命令超时的代码
Add to the target: 添加到目标:
dbTarget.Parameters.Add(new DatabaseParameterInfo("@LogDate", new NLog.Layouts.SimpleLayout("${event-properties:CommandTimeout}")));
When writing the event (using the fluent interface, see 3 ) 编写事件时(使用流畅的界面,请参阅3 )
var logger = LogManager.GetCurrentClassLogger();
logger.Info()
.Message("This is a test fluent message '{0}'.", DateTime.Now.Ticks)
.Property("CommandTimeout", YourCommandTimeout)
.Write();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.