Using NLog 5.0, NLog.Web 5.0 and NLog.Database 5.0. I am attempting to log the aspnet request variables as described in the documentation. I log everything else just fine. Here is the code to setup the logger. I also included the internal log file which I had set to Trace.
var config = new LoggingConfiguration();;
DatabaseTarget dbTarget = new DatabaseTarget()
{
ConnectionString = connString,
DBProvider = "sqlserver",
Name = "database",
CommandText = "sp_insert_log",
CommandType = System.Data.CommandType.StoredProcedure
};
dbTarget.Parameters.Add(new DatabaseParameterInfo("@browser", "${aspnet-request-useragent}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@ip_address", "${aspnet-request-ip}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@url", "${aspnet-request-url}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@message", "${message}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@level", "${level}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@stack_trace", "${stacktrace:format=DetailedFlat}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@exception", "${exception:format=tostring}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@logger", "${logger}"));
config.AddTarget("database", dbTarget);
config.LoggingRules.Add(new LoggingRule("*", ConfigHelper.CommonLogLevel, dbTarget));
InternalLogger.LogFile = @"D:\logs\internal\" ConfigHelper.InternalLogFile;
InternalLogger.LogLevel = ConfigHelper.InternalLogLevel;
InternalLogger.IncludeTimestamp = true;
LogManager.ThrowConfigExceptions = ConfigHelper.ThrowNlogConfigExceptions;
LogManager.ThrowExceptions = ConfigHelper.ThrowNlogExceptions;
LogManager.Configuration = config;
In a controller Action: ....
try
{
//...attempt stuff
}
catch(Exception ex)
{
var CommonLogger = LogManager.GetCurrentClassLogger();
CommonLogger.Error(ex);
}
....
Trace output from the internal log:
2022-07-06 15:11:32.6517 Trace FindReachableObject<NLog.Internal.IRenderable>:
2022-07-06 15:11:32.6517 Trace Scanning SimpleLayout ' => '
2022-07-06 15:11:32.6517 Trace Scanning Property LayoutRenderers 'NLog.LayoutRenderers.LayoutRenderer[]' System.Collections.Generic
2022-07-06 15:11:32.6517 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2022-07-06 15:11:32.7450 Trace DatabaseTarget(Name=database): Open connection.
2022-07-06 15:11:32.7450 Trace DatabaseTarget(Name=database): Executing StoredProcedure: sp_insert_log
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: **'@browser' = ''** (String)
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: **'@ip_address' = ''** (String)
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: **'@url' = ''** (String)
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: '@message' = 'System.Exception: Extended data test' (String)
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: '@level' = 'Error' (String)
2022-07-06 15:11:32.7520 Trace FindReachableObject<NLog.Internal.IRenderable>:
2022-07-06 15:11:32.7520 Trace Scanning SimpleLayout ' => '
2022-07-06 15:11:32.7520 Trace Scanning Property LayoutRenderers 'NLog.LayoutRenderers.LayoutRenderer[]' System.Collections.Generic
2022-07-06 15:11:32.7520 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: '@stack_trace' = '[System.Object Execute(System.Web.Mvc.ControllerBase, System.Object[])] => [System.Object lambda_method(System.Runtime.CompilerServices.Closure, System.Web.Mvc.ControllerBase, System.Object[])] => [System.Web.Mvc.ActionResult Login(MyApp.Application.DTO.UserLoginDTO)]' (String)
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: '@exception' = 'System.Exception: Extended data test
at MyApp.Web.Controllers.LoginController.Login(UserLoginDTO dto) in MyApp\MyApp.Web\Controllers\LoginController.cs:line 31' (String)
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: '@logger' = 'MyApp.Web.Controllers.BaseController' (String)
2022-07-06 15:11:32.7520 Trace DatabaseTarget: Parameter: '@extended_data' = '' (String)
2022-07-06 15:11:32.8397 Trace DatabaseTarget(Name=database): Finished execution, result = 1
2022-07-06 15:11:32.8397 Trace DatabaseTarget(Name=database): Close connection (KeepConnection = false).
Any thoughts on what I could be doing wrong?
CodePudding user response:
If you enabled the NLog InternalLogger as the first thing (Before new LoggingConfiguration()
), then you would probably see why it fails.
If you enabled ThrowConfigExceptions as the very first thing, then you would probably get a nice exception:
NLog.LogManager.ThrowConfigExceptions = true;
I'm guessing the problem is that you have not registered the NLog.Web-extension before using them in DatabaseParameterInfo
:
NLog.LogManager.Setup().SetupExtensions(s =>s.RegisterAssembly("NLog.Web"));
See also: https://github.com/NLog/NLog/wiki/Register-your-custom-component