Reputation: 764
I encountered issue using NLog when I want to render asp.net related info into my database. Here is the internal logging for NLog.
2017-07-16 03:08:06.5485 Debug ScanAssembly('NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=')
2017-07-16 03:08:06.5796 Debug Start auto loading, location:
2017-07-16 03:08:06.5796 Debug Auto loading done
2017-07-16 03:08:06.6036 Error Error parsing layout aspnet-request-method will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-request-method'. Is NLog.Web not included?
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)
2017-07-16 03:08:06.6115 Error Error parsing layout aspnet-request-method will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-request-method'. Is NLog.Web not included?
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)
2017-07-16 03:08:06.6115 Debug --- NLog configuration dump ---
2017-07-16 03:08:06.6115 Debug Targets:
2017-07-16 03:08:06.6276 Debug Rules:
2017-07-16 03:08:06.6276 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] appendTo: [ ]
2017-07-16 03:08:06.6276 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] appendTo: [ ]
2017-07-16 03:08:06.6276 Debug --- End of NLog configuration dump ---
2017-07-16 03:08:06.6456 Info Found 24 configuration items
2017-07-16 03:08:06.6966 Info Found 24 configuration items
2017-07-16 03:08:06.7085 Debug Targets for LicenseServer.Utility.Log by level:
2017-07-16 03:08:06.7085 Debug Trace =>
2017-07-16 03:08:06.7085 Debug Debug =>
2017-07-16 03:08:06.7225 Debug Info =>
2017-07-16 03:08:06.7225 Debug Warn =>
2017-07-16 03:08:06.7225 Debug Error =>
2017-07-16 03:08:06.7225 Debug Fatal =>
I am avoiding doing configuration on xml file. so I took programmatic configuration approach. which is working fine totally except it is giving issue when I put 'aspnet-request-method' and any other aspnet related layouts. I installed NLog.Web and NLog.Extend but it is still not working. below is my code on setting up configuration :
public static class Log
{
public static Logger _logger { get; set; }
public static void Init()
{
InternalLogger.LogFile = @"D:\Nlog.txt";
InternalLogger.LogLevel = LogLevel.Debug;
var config = new LoggingConfiguration();
var db_target = new DatabaseTarget();
db_target.DBProvider = @"MySql.Data.MySqlClient.MySqlConnection, MySql.Data";
db_target.ConnectionString =
$@"server={DBConfig._server};database={DBConfig._dbname};userid={DBConfig._username};password={DBConfig
._password};";
db_target.DBHost = DBConfig._server;
db_target.DBDatabase = DBConfig._dbname;
db_target.DBUserName = DBConfig._username;
db_target.DBDatabase = DBConfig._password;
db_target.CommandText = @"INSERT INTO error_log(system, module, action, content) VALUES(@activityid, @request, @logger, @ip)";
db_target.Parameters.Add(new DatabaseParameterInfo("@activityid", new NLog.Layouts.SimpleLayout("${activityid}")));
db_target.Parameters.Add(new DatabaseParameterInfo("@request", new NLog.Layouts.SimpleLayout(@"${aspnet-request-method}")));
db_target.Parameters.Add(new DatabaseParameterInfo("@logger", new NLog.Layouts.SimpleLayout("${logger}")));
db_target.Parameters.Add(new DatabaseParameterInfo("@ip", new NLog.Layouts.SimpleLayout("${aspnet-request-method}")));
db_target.KeepConnection = true;
var rule = new LoggingRule("*", LogLevel.Debug, db_target);
config.LoggingRules.Add(rule);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
_logger = LogManager.GetCurrentClassLogger();
}
}
Upvotes: 4
Views: 4220
Reputation: 36750
Please note, you need to install NLog.Web (ASP.NET non-core) or NLog.Web.AspNetCore and for ASP.NET Core you need in your nlog.config
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
Upvotes: 3