user2661305
user2661305

Reputation: 381

NLog not writing to eventlog .NET Core 2.1

I've added NLog to my .NET core console app and it works with file and database targets. However when i try and get it to work with eventviewer it doesn't log anything. When i add the code for the eventviewer target the file and database part doesn't log anything. When I remove it, the logging starts working again.

I have added a new event viewer source for the application using Powershell, so that wouldn't be the issue. The application doesn't crash or report an error, it runs fine but doesn't log anything when event viewer is included.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  autoReload="true"
  internalLogFile="c:\temp\console-example-internal.log"
  internalLogLevel="Info" >

<targets>    
  <target xsi:type="File" name="logfile" fileName="c:\temp\console-example.log"
        layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-e
        vent-properties}" />

<target xsi:type="Console" name="console"
        layout="[${longdate}][${machinename}][${level:uppercase=true}] ${message} ${exception}" />

<target xsi:type="EventLog" name="eventlog" source="testlogging" log="Application"
        layout="${message}${newline}${exception:format=ToString}" />

  <target xsi:type="Database" name="database" >
      <connectionString>Server=test; Database=test; User Id=sa; Password=password;</connectionString>
      <commandText>
          INSERT INTO dbo.Log (Application, Logged, Level, Message, Logger, CallSite, Exception ) 
          VALUES (@Application, @Logged, @Level, @Message, @Logger, @Callsite, @Exception);
      </commandText>
      <parameter name="@application" layout="TestLoggingApp" />
      <parameter name="@logged" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="url: ${aspnet-request-url} | action: ${aspnet-mvc-action} | ${message}" />

      <parameter name="@logger" layout="${logger}" />
      <parameter name="@callSite" layout="${callsite:filename=true}" />
      <parameter name="@exception" layout="${exception:tostring}" />
  </target>    
</targets>

<rules>
   <logger name="*" minlevel="Trace" writeTo="logfile, console, database" />        
</rules>
</nlog>

Any ideas on how to implement this or if I have missed something?

Upvotes: 3

Views: 2913

Answers (2)

Jim Neff
Jim Neff

Reputation: 326

For me the answer was adding "NLog.WindowsEventLog" assembly to the extensions area to nlog.config:

<extensions>
   <add assembly="NLog.WindowsEventLog" />
</extensions>

I had the default: "NLog.Web.AspNetCore".

Upvotes: 7

Rolf Kristensen
Rolf Kristensen

Reputation: 19847

.Net Core is a subset of the .Net Framework, and doesn't include things like Windows EventLog (as this doesn't exist on other non-Windows-devices).

You should make sure to check the platform support documentation first:

https://github.com/NLog/NLog/wiki/platform-support

There you will see that a special Nuget-package has been created for .Net Core-applications on the Windows platform:

https://www.nuget.org/packages/NLog.WindowsEventLog/

Upvotes: 1

Related Questions