NLog for ASP MVC not writing to the database - c#

I've tried to implement NLog in my application. However, for some reason nothing is being written to the database. I have tried to debug a simple info and noticed that I have this error :
- DeclaringMethod '((System.RuntimeType)log._loggerType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException' System.Reflection.MethodBase {System.InvalidOperationException}
Not sure how this is relevant.
I have switched on internal debugging as well and have not received any noticeable errors. This is the internal debug log:
2018-07-23 00:25:37.1679 Trace Scanning Property Layout ''${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}'' NLog.Layouts
2018-07-23 00:25:37.1679 Trace Scanning SimpleLayout ''${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}''
2018-07-23 00:25:37.1868 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.1868 Trace Scanning Property Layout ''${logger}'' NLog.Layouts
2018-07-23 00:25:37.2058 Trace Scanning SimpleLayout ''${logger}''
2018-07-23 00:25:37.2148 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.2148 Trace Scanning Property Layout ''${callsite}'' NLog.Layouts
2018-07-23 00:25:37.2367 Trace Scanning SimpleLayout ''${callsite}''
2018-07-23 00:25:37.2457 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.2457 Trace Scanning Property Layout ''${exception:tostring}'' NLog.Layouts
2018-07-23 00:25:37.2687 Trace Scanning SimpleLayout ''${exception:tostring}''
2018-07-23 00:25:37.2887 Trace Database Target[database] has 16 layouts
2018-07-23 00:25:37.3007 Trace FindReachableObject<System.Object>:
2018-07-23 00:25:37.3117 Trace Scanning SimpleLayout ''Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-ManagementStudio-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ManagementStudio-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c.mdf''
2018-07-23 00:25:37.3117 Trace Scanning Property Renderers 'System.Collections.ObjectModel.ReadOnlyCollection`1[NLog.LayoutRenderers.LayoutRenderer]' System.Collections.ObjectModel
2018-07-23 00:25:37.3296 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-07-23 00:25:37.4832 Debug Watching path 'C:\Users\amosa\Source\Repos\ManagementStudio\ManagementStudio' filter 'NLog.config' for changes.
2018-07-23 00:25:37.4936 Info Configuration initialized.
2018-07-23 00:25:37.5095 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.5.7.7958. Product version: 4.5.7.
2018-07-23 00:25:37.5095 Debug Targets for ManagementStudio.Controllers.UsersController by level:
2018-07-23 00:25:37.5304 Debug Trace =>
2018-07-23 00:25:37.5398 Debug Debug =>
2018-07-23 00:25:37.5398 Debug Info =>
2018-07-23 00:25:37.5598 Debug Warn =>
2018-07-23 00:25:37.5768 Debug Error =>
2018-07-23 00:25:37.5768 Debug Fatal =>
Here is my nlog.config file. I have created the database table based on what this config shows:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<target name="database" xsi:type="Database">
<connectionStringName>DefaultConnection</connectionStringName>
<commandText>
insert into dbo.AspNetEventLogs (
Application, Logged, Level, Message,
Username,
ServerName, Port, Url, Https,
ServerAddress, RemoteAddress,
Logger, CallSite, Exception
) values (
#Application, #Logged, #Level, #Message,
#Username,
#ServerName, #Port, #Url, #Https,
#ServerAddress, #RemoteAddress,
#Logger, #Callsite, #Exception
);
</commandText>
<parameter name="#application" layout="${appsetting:name=appName}" />
<parameter name="#logged" layout="${date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#message" layout="${message}" />
<parameter name="#username" layout="${identity}" />
<parameter name="#serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
<parameter name="#port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="#url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="#https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
<parameter name="#serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="#remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#callSite" layout="${callsite}" />
<parameter name="#exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="database" minlevel="Debug" writeTo="database" />
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>

The name of the logger-rules is actually a wildcard for matching logger-names. Try changing to a star instead:
<rules>
<logger name="*" minlevel="Debug" writeTo="database" />
<rules>
See also https://github.com/NLog/NLog/wiki/Configuration-file#rules

Related

Unable to suppress Microsoft logs with NLog for API calls using HttpClient

I have implemented NLog for .NET Core 3.1 MVC web app and API projects. The nlog.config file for both of them is nearly identical. I've carefuly checked this and the only difference is the database table name and file name that they log to.
I'm successfully suppressing non-essential Microsoft logs from the API project, but am only partially able to do so for the web app. Specifically, Trace and Info logs by Microsoft that deal with making calls to the API using HttpClient appear in the file and database:
You can see that only one of the logs is a log that I actually wrote, and the rest are were automatically logged by Microsoft.
I'm not sure what to try here, but I've looked at the internal logs and saw nothing out of the ordinary.
I want to know how can I suppress the additional Microsoft logs and what's wrong with my current configuration?
NuGet packages I have installed (identical for both apps):
nlog.config of the web app project:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwConfigExceptions="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-CNC_WebUI-nlog.txt">
<extensions>
<add assembly="Nlog.Extensions.Logging"/>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target xsi:type="File" name="CNC_WebUIFile" fileName="c:\temp\nlog-CNC_WebUIFile-${shortdate}.log"
layout="${aspnet-traceidentifier}|${configsetting:AppSettings.NlogConnection.AppName}|${event-properties:ObjectID}|${date}|${uppercase:${level}}|${message} ${exception:format=tostring}|${logger}|${callsite:filename=false}|${exception:toString}" />
<target name="databaseLogger" xsi:type="Database"
dbProvider="sqlserver"
dbHost="${configsetting:AppSettings.NlogConnection.DbHost}"
dbDatabase="${configsetting:AppSettings.NlogConnection.Database}"
dbUserName="${configsetting:AppSettings.NlogConnection.User}"
dbPassword="${configsetting:AppSettings.NlogConnection.Password}" >
<commandText>
INSERT INTO dbo.CNC_WebUILogs (
CorrelationId, Application, ObjectID, Logged, Level, Message,
Logger, CallSite, Exception
) VALUES (
#CorrelationId, #Application, #ObjectID, #Logged, #Level, #Message,
#Logger, #Callsite, #Exception
);
</commandText>
<parameter name="#correlationId" layout="${aspnet-traceidentifier}" />
<parameter name="#application" layout="${configsetting:AppSettings.NlogConnection.AppName}" />
<parameter name="#ObjectID" layout="${event-properties:ObjectID}" />
<parameter name="#logged" layout="${date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#message" layout="${message}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#callSite" layout="${callsite:filename=false}" />
<parameter name="#exception" layout="${exception:toString}" />
</target>
</targets>
<rules>
<logger name="Microsoft.*" levels="Warn,Error,Fatal" writeTo="databaseLogger,CNC_WebUIFile"></logger>
<logger name="Microsoft.*" minlevel="Trace" final="true" />
<logger name="*" minlevel="Trace" writeTo="CNC_WebUIFile" />
<logger name="*" minlevel="${configsetting:AppSettings.NlogConnection.LogLevel}" writeTo="databaseLogger" />
</rules>
</nlog>
One of my repos that had their API call activity logged by Microsoft:
public class MfgrRepo : IMfgrRepo
{
private readonly IHttpClientFactory _clientFactory;
public MfgrRepo(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task<List<MfgrDto>> Get()
{
HttpClient client = _clientFactory.CreateClient(HttpClientConfigNames.CNC);
List<MfgrDto> models = new List<MfgrDto>();
try
{
HttpResponseMessage response = await client.GetAsync("api/Mfgrs");
models.AddRange(await response.Content.ReadFromJsonAsync<List<MfgrDto>>());
}
catch (Exception ex)
{
throw ex;
}
return models;
}
}
You can configure the log levels that the M.E.Logging system uses through the appsettings.json file. This also allows you to configure the types per namespace, so you can make the System.Net.Http namespace silent that way:
{
"Logging": {
"LogLevel": {
"System.Net.Http": "Warning"
}
}
}
You already have one blackhole here:
<rules>
<logger name="Microsoft.*" levels="Warn,Error,Fatal" writeTo="databaseLogger,CNC_WebUIFile"></logger>
<logger name="Microsoft.*" minlevel="Trace" final="true" /> <!-- Black Hole -->
<logger name="*" minlevel="Trace" writeTo="CNC_WebUIFile" />
<logger name="*" minlevel="${configsetting:AppSettings.NlogConnection.LogLevel}" writeTo="databaseLogger" />
</rules>
I would just configure an additional "blackhole" like this:
<rules>
<logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- Black Hole 1 -->
<logger name="System.Net.Http.*" maxLevel="Info" final="true" /> <!-- Black Hole 2 -->
<logger name="*" minlevel="Trace" writeTo="CNC_WebUIFile" />
<logger name="*" minlevel="${configsetting:AppSettings.NlogConnection.LogLevel}" writeTo="databaseLogger" />
</rules>

NLog seems to be writing incorrect value in database

I'm using NLog for logging into database. It seems to me its misplacing value in columns. For instance, it writes StackTrace in Message column and Exception information in StackTrace column
Configuration:
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwConfigExceptions="true" throwExceptions="true">
<targets>
<target name="database" type="Database" connectionString="Data Source=Server1;initial catalog=MyDb;Integrated Security=True;">
<commandText>insert into dbo.AppException ([Level], Logger, Message, Exception, StackTrace) values (#Level, #Logger, #Message, #Exception, #StackTrace);</commandText>
<parameter name="#Level" layout="${level}" />
<parameter name="#Logger" layout="${logger}" />
<parameter name="#Message" layout="${message}" />
<parameter name="#Exception" layout="${exception}" />
<parameter name="#StackTrace" layout="${stacktrace}" />
<dbProvider>System.Data.SqlClient</dbProvider>
</target>
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="database" />
</rules>
</nlog>
My test code:
throw new IOException("This is my message");
Logging code:
logger.Error(ex);
Below is a sample row in database
In my opinion, the value in "Exception" field should be written in "Message" column and value of "StackTrace" should be written into "Exception" column and finally value of "Message" should be written in "StackTrace".
Is there anything wrong in my configuration or my expectation is wrong?
I'm guessing you are logging the exception like this:
catch (Exception ex)
{
_logger.Error(ex); // ${message} will become ex.ToString(), since no message provided.
}
If you changed to this instead:
catch (Exception ex)
{
_logger.Error(ex, "Exception caught while testing");
}
And updated NLog.config to this:
<parameter name="#Exception" layout="${exception:format=tostring,data}" />
Then you will probably get what you want.
After reading answer posted by #Rolf, I found my nlog.config setting is not correct. The format setting in nlog is important
NLog Document
I changed my nlog to below and it worked as expected
<parameter name="#Message" layout="${exception:format=message}" />
<parameter name="#Exception" layout="${exception:format=type}" />
<parameter name="#StackTrace" layout="${exception:format=stacktrace}" />

NLog.Web.AspNetCore no longer logging to database

I have a dotnet core API that's up on version 2.2 with NLog.Web.AspNetCore 4.7.0 (NLog 4.5.11) which was previously working back in version 4.5.4. With the update, it now doesn't appear to be logging to the database. Nothing in terms of the logging infrastructure has changed within my code.
I should mention, I also have in the log this lovely line, so I know it's somewhat working. But I use to get A LOT more information, and of course, the database would have stuff written to it as well.
2018-12-26 15:06:59.9503||DEBUG|SomeApp.API.Program|init main |url: |action:
I tried looking around in the github issues and on here, but I haven't found anything of much use, any help is greatly appreciated!
Initial Setup in Program.cs
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
}
catch (Exception ex)
{
//NLog: catch setup errors
logger.Error(ex, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
Implementation
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseIISIntegration()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog() // NLog: setup NLog for Dependency injection
.Build();
nlog config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Trace"
internalLogFile="C:\temp\internal-nlog.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets async="true">
<target type="File" name="file" fileName="${basedir}\logs\logfile.txt"
maxArchiveFiles="5" archiveAboveSize="20971520" archiveEvery="Day"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="Null" name="blackhole" />
<target name="database" xsi:type="Database">
<connectionString>${gdc:item=connectionString}</connectionString>
<commandText>
insert into dbo.Log (
EventId, Logged, Level, Message,
Username,
ServerName, Port, Url, Https,
ServerAddress, RemoteAddress,
Logger, CallSite, Exception
) values (
#EventId, #Logged, #Level, #Message,
#Username,
#ServerName, #Port, #Url, #Https,
#ServerAddress, #RemoteAddress,
#Logger, #Callsite, #Exception
);
</commandText>
<parameter name="#EventId" layout="${event-properties:item=EventId_Id}" />
<parameter name="#logged" layout="${date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#message" layout="${message}" />
<parameter name="#username" layout="${aspnet-user-identity}" />
<parameter name="#serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
<parameter name="#port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="#url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="#https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
<parameter name="#serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="#remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#callSite" layout="${callsite}" />
<parameter name="#exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<!--Limit to only configured logs for all logs, change name="*"-->
<logger name="SomeApp.*" minlevel="Info" writeTo="database" />
<logger name="*" minlevel="Trace" writeTo="file" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
</rules>
</nlog>
Sample logging code
_logger.LogInformation(1001, "Login success: {0}", userForLoginDto.Username);
appsettings.json & appsettings.Development.json Logging Settings
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Information"
}
The NLog internal log doesn't seem to have anything of significance, I can provide it if requested though.
Apologies for any weird formatting issues, the nlog.config file did not want to come willingly.

NLog to Database (Oracle)

I'm trying to use NLog to log to an Oracle database, already created the table but when I try to log something I get the exception:
ORA-00928: missing SELECT keyword
My NLog.config file is:
<?xml version="1.0" ?>
<nlog autoReload="true" throwExceptions="true" internalLogFile="${basedir}/App_Data/nlog.txt" internalLogLevel="Debug"
internalLogToConsole="true">
<targets>
<!--Useful for debugging-->
<target name="filelog" type="File" fileName="C:/App_Data/Site.log"
layout="${date}: ${message}" />
<target name="databaselog" type="Database">
<dbProvider>Oracle.DataAccess.Client</dbProvider>
<!-- database connection parameters -->
<!-- alternatively you could provide a single 'connectionstring' parameter -->
<connectionString>DATA SOURCE=database;PERSIST SECURITY INFO=True;USER ID=user;Password=password;Validate Connection=true</connectionString>
<commandText>
insert into RS_LOGTABLE ([log_user],[log_level],[log_date],[log_message]) values(#log_user,#log_level,#log_date,#log_message);
</commandText>
<parameter name="#log_user" layout="${message}"/>
<parameter name="#log_level" layout="${message}"/>
<parameter name="#log_date" layout="${date}"/>
<parameter name="#log_message" layout="${message}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="filelog" />
<logger name="*" minlevel="Trace" writeTo="databaselog" />
</rules>
</nlog>
And the exception occurs when I do this:
logger = LogManager.GetCurrentClassLogger();
logger.Debug("Teste logger");
I already tried to do the insert without the brackets and get another exception:
**ORA-00936: missing expression**
I guess it's bit late for you, but I hope my solution will be helpful for others.
I changed characters # for colons at commandText and at parameter tags I removed it completely. I don't use brackets and it started to work.
It should be correct this way:
<commandText>
insert into RS_LOGTABLE (log_user,log_level,log_date,log_message) values(:log_user,:log_level,:log_date,:log_message);
</commandText>
<parameter name="log_user" layout="${message}"/>
<parameter name="log_level" layout="${message}"/>
<parameter name="log_date" layout="${date}"/>
<parameter name="log_message" layout="${message}"/>
I tested it with dbProvider Oracle.DataAccess.Client.
Warning for others:
I had a variable named with reserved word and it throws another exception. More details are here PHP ORA-01745: invalid host/bind variable name Warning

NLog not writing debug messages

In my code I have some info messages like logger.Log("dwewe") and logger.Debug("ddddf").
The problem is that the Debug messages are not being written even when I debug in VS.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true"
internalLogLevel="Trace"
internalLogFile="c:\nlog-app.log"
autoReload="false"
internalLogToConsole="true">
<!--
See http://nlog-project.org/wiki/Configuration_file
for information on customizing logging rules and outputs.
-->
<targets>
<!-- file targets -->
<target name="asyncFile" xsi:type="AsyncWrapper">
<target xsi:type="File" name="f" fileName="${basedir}/Logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message} ${event-context:item=error-source} ${event-context:item=error-class} ${event-context:item=error-method} ${event-context:item=error-message} ${event-context:item=inner-error-message} ${event-context:item=stack-trace}"/>
</target>
<!-- database targets -->
<target name="database" xsi:type="Database" keepConnection="true" useTransactions="true"
dbProvider="System.Data.SqlClient"
connectionString="data source=XXXXXX.database.windows.net;initial catalog=NLog;integrated security=false;persist security info=True;User ID=XXXXr;Password=BXXXX3"
commandText="INSERT INTO Logs(EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage, StackTrace) VALUES (#EventDateTime, #EventLevel, #UserName, #MachineName, #EventMessage, #ErrorSource, #ErrorClass, #ErrorMethod, #ErrorMessage, #InnerErrorMessage, #StackTrace)">
<!-- parameters for the command -->
<parameter name="#EventDateTime" layout="${date:s}" />
<parameter name="#EventLevel" layout="${level}" />
<parameter name="#UserName" layout="${windows-identity}" />
<parameter name="#MachineName" layout="${machinename}" />
<parameter name="#EventMessage" layout="${message}" />
<parameter name="#ErrorSource" layout="${event-context:item=error-source}" />
<parameter name="#ErrorClass" layout="${event-context:item=error-class}" />
<parameter name="#ErrorMethod" layout="${event-context:item=error-method}" />
<parameter name="#ErrorMessage" layout="${event-context:item=error-message}" />
<parameter name="#InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
<parameter name="#StackTrace" layout="${event-context:item=stack-trace}" />
</target>
<target name="console" xsi:type="Console" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Info" writeTo="database" />
<logger name="*" minlevel="Error" writeTo="asyncFile" />
<logger name="*" minlevel="Trace" writeTo="console" />
</rules>
</
The reason that you are not able to get Debug is that debug is the lowest level log level
just add following tag in rules tag in nlog.config file.
<logger name="*" minlevel="Debug" writeTo="console" />
I found the problem to be related to the default appsettings.json that Visual Studio automatically adds to the project. It looks like this.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
The solution was to remove or rename this section.
{
"Logging-DISABLE": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
This allowed nlog.config to be utilized.
You are using 3 different log targets.
Database target is set to Info level, so debug messages are not going there.
File target accepts only Error messages (and higher) so there will not be any debug either.
The last target Console is the one where debug mesages should be looged to. But as I see it you didn't set layout of the message. Try to look at this documentation. It says that layout is a required field.
Also I would suggest you temporarily set additional File target and set it to accept debug messages.
For anyone else who has this issue, an answer at a similar question just saved me: https://stackoverflow.com/a/8881521/3959735
If you are using a separate NLog.config file, make sure it's set to "copy always" via its file properties. Alternatively, you can include the NLog configuration section in your main App.config.
I think I might have caused this issue for myself either by trying to copy an NLog configuration file from another project manually; or because when adding NLog to my project I got a permissions error (of which I cannot remember any specific details) – just passing on this information in case it helps anyone diagnose their own issue.
Add nlog.config file to your project
Example of my config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}/Logs/${date:format=yyyy-MM-dd}_log.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
</configuration>

Categories

Resources