问题描述
我有2个NLog数据库目标.第一个一直在工作.当我检查数据库中的表时,我刚刚添加的第二个似乎并没有保存.但是,我也没有例外要进行调试..是否可以打开某些内容?还是我需要看的地方?
I have 2 NLog database targets. The first one has been working. The second one that I just added does not seem to save when I check the table in the database.. However, I get no exception to look at for debugging.. Is there something I can turn on? Or somewhere I need to look?
也许如果有NLog专家,他们可以看到我可能犯的一个简单错误吗?这是我用于创建NLog配置的代码:
Maybe if there are any NLog experts, they can see a simple mistake I may have made? Here is my code for creating the NLog configuration:
// nlog config (ExceptionTracking)
LogManager.ThrowExceptions = true;
// Using NLog.Common
InternalLogger.LogLevel = LogLevel.Warn;
InternalLogger.LogFile = @"c:\log.txt";
var config = new LoggingConfiguration();
var dbTarget = new DatabaseTarget()
{
Name = "ExceptionTracking",
ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NLogConnection"].ConnectionString,
CommandText = "INSERT INTO Common.ExceptionTracking ( ApplicationID, Url, IP, Referrer, UserName, ExceptionDescription, Action, Controller, InsertedBy, InsertedDate) " +
"VALUES (@ApplicationID, @Url, @IP, @Referrer, @UserName, @ExceptionDescription, @Action, @Controller, @InsertedBy, @InsertedDate);"
};
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@ApplicationID", Layout = "${appsetting:name=AppID:default=null}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@Url", Layout = "${aspnet-Request-Url}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@IP", Layout = "${aspnet-Request-IP}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@Referrer", Layout = "${aspnet-Request-Referrer}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@UserName", Layout = "${identity:authType=false:isAuthenticated=false}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@ExceptionDescription", Layout = "${exception:tostring}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@Action", Layout = "${aspnet-MVC-Action}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@Controller", Layout = "${aspnet-MVC-Controller}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@InsertedBy", Layout = "${gdc:BEMSID}" });
dbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@InsertedDate", Layout = "${date}" });
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, dbTarget));
// Add another NLog Target (ChangeTracking)
var changeTrackingDbTarget = new DatabaseTarget()
{
Name = "ChangeTracking",
ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NLogConnection"].ConnectionString,
CommandText = "INSERT INTO Common.ChangeTracking ( ApplicationID, UserName, Name, EntityName, PropertyName, PrimaryKeyValue, OldValue, NewValue, DateChanged) " +
"VALUES (@ApplicationID, @UserName, @Name, @EntityName, @PropertyName, @PrimaryKeyValue, @OldValue, @NewValue, @DateChanged);"
};
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@ApplicationID", Layout = "${appsetting:name=AppID:default=null}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@UserName", Layout = "${identity:authType=false:isAuthenticated=false}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@Name", Layout = "${event-properties:item=name}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@EntityName", Layout = "${event-properties:item=entityname}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@PropertyName", Layout = "${event-properties:item=propname}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@PrimaryKeyValue", Layout = "${event-properties:item=primarykey}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@OldValue", Layout = "${event-properties:item=oldvalue}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@NewValue", Layout = "${event-properties:item=newvalue}" });
changeTrackingDbTarget.Parameters.Add(new DatabaseParameterInfo() { Name = "@DateChanged", Layout = "${date}" });
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Off, changeTrackingDbTarget));
LogManager.Configuration = config;
LogManager.Configuration.AddTarget("ExceptionTracking", dbTarget);
LogManager.Configuration.AddTarget("ChangeTracking", changeTrackingDbTarget);
}
以下是尝试实际创建日志的代码:
Here is the code that trys to actually create a log:
private static Logger _logger = LogManager.GetLogger("ChangeTracking");
LogEventInfo changeEvent = new LogEventInfo(LogLevel.Info, "ChangeTracking", "A change event has been fired");
changeEvent.Properties["Name"] = "Jake";
changeEvent.Properties["EntityName"] = "test";
changeEvent.Properties["PropertyName"] = "test property name";
changeEvent.Properties["PrimaryKeyValue"] = "test";
changeEvent.Properties["OldValue"] = "test";
changeEvent.Properties["NewValue"] = "test";
_logger.Log(changeEvent);
推荐答案
throwExceptions
您可以启用NLog引发异常.配置:
throwExceptions
You could enable that NLog will throw exceptions. Config:
<nlog throwExceptions="true" >
或在代码中:
LogManager.ThrowExceptions = true
内部日志
您可以启用内部日志.这将通过NLog在内部显示日志.在这种情况下,警告(和警告)应该足够了.
Internal log
You could enable the internal log. This will show the log internally by NLog. In this case warning (and upper) should be enough.
配置:
<nlog internalLogFile="c:\log.txt" internalLogLevel="Warn">
使用代码:
// Using NLog.Common
InternalLogger.LogLevel = LogLevel.Warn;
InternalLogger.LogFile = @"c:\log.txt";
在此处
还有一个 NLog故障排除指南
这篇关于我的NLog记录未保存.在哪里可以看到我的NLog保存失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!