Reputation: 610
I'm trying to log only Errors into the Windows syslog (events viewer), I tried many solutions but none of them run as expected. I still see warning messages in the Windows syslog.
It's an Asp.Net Core project (nuget 2.2.0) with NLog 5.2.8, NLog.Web.AspNetCore, Version=4.0.0.0. I don't have any a content related to log configuration in my appsettings.json file and here is the content of my nglog.config file.
What am I missing?
<?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">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.WindowsEventLog"/>
</extensions>
<targets>
<target xsi:type="File" name="allfile" fileName="Backoffice_template.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring} |action: ${aspnet-mvc-action} |user: ${aspnet-user-identity}" archiveEvery="Day" encoding="utf-8"
archiveNumbering = "Date"
archiveDateFormat = "yyyyMMdd_HHmmss" archiveOldFileOnStartup="false" maxArchiveDays="30" />
<target name="EventLog" xsi:type="EventLog">
<logLevel>Error</logLevel>
</target>
<target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" maxLevel="Warning" writeTo="allfile" />
<logger name="Microsoft.*" maxLevel="Warning" final="true" />
<logger name="System.Net.Http.*" maxlevel="Info" />
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />
<logger name="*" minlevel="Error" writeTo="EventLog" final="true" />
</rules>
</nlog>
Logger is created like this:
var logger = LogManager.Setup().GetCurrentClassLogger();
Strange fact, it's running as expected if I add this to appsettings.json:
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Error"
}
}
},
And if I use my original nlog.config file:
<?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"
>
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target xsi:type="File" name="allfile"
fileName="Backoffice_template.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring} |action: ${aspnet-mvc-action} |user: ${aspnet-user-identity}"
archiveEvery="Day"
encoding="utf-8"
archiveNumbering = "Date"
archiveDateFormat = "yyyyMMdd_HHmmss"
archiveOldFileOnStartup="false"
maxArchiveDays="30"
/>
<target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />
</targets>
<rules>
<!--Do not change this value to set log level for BackOffice, instead change LogLevel.Default in appsettings.json -->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="Microsoft.*" maxLevel="Warning" final="true" />
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole" final="true" />
</rules>
</nlog>
In summary, I have been asked to move the eventlog configuration from the appsettings.json to the NLog but I don't succeed to to it.
Upvotes: 0
Views: 110
Reputation: 610
As stated on this page, when using Asp.Net Core (on Windows), there are some log providers that are automatically added, so trying to configure then in NLog is useless:
The default ASP.NET Core web app templates call WebApplication.CreateBuilder, which adds the following logging providers:
Console Debug EventSource EventLog: Windows only
Upvotes: 0