I'm trying to get my console application to log to the database, file, console and eventviewer. However everytime i had the eventviewer code it throws an exception. I added the NLog.WindowsEventLog nuget package but it still isnt working. The errors can be seen below and the full config image is attached:
2018-08-01 Error parsing layout all-event-properties will be ignored. Exception: LayoutRenderer cannot be found: 'all-event-properties' at NLog.Config.Factory
2018-08-01 Error Parsing configuration from C:\Users\user20\TestLogging\TestLogging\nlog.config failed. Exception: Exception when parsing C:\Users\user20\TestLogging\TestLogging\nlog.config. ---> Target cannot be found: 'EventLog' at NLog.Config.Factory
Try remove everything from your NLog.config except the EventLog-target and a single logging-rule. Instead of trying to fly on first attempt. Then work from something simple like all developers do:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
internalLogFile="c:\temp\console-example-internal.log"
internalLogLevel="Info" >
<extensions>
<add assembly="NLog.WindowsEventLog" />
</extensions>
<targets>
<target xsi:type="EventLog" name="eventlog" source="testlogging" log="Application"
layout="${message}${newline}${exception:format=ToString}" />
</targets>
<rules>
<logger name="*" minLevel="Trace" writeTo="eventlog" />
</rules>
</nlog>
Have you checked that the NLog.WindowsEventLog.dll is deployed with your application?
Have you checked that internal logger whether it recognizes and loads the NLog.WindowsEventLog-assembly?
P.S. Still see that you have not removed the fault newline from the definition of logfile-target
Sorry I had removed the new line yesterday when trying to get this to work and made other tweaks, when they didnt work i undone my changes and reverted it back to the original code. Thats why you still see the newline.
I've striped down the config and made it only to be what you have said but it still fires up an error saying _Target cannot be found: 'EventLog'_. I'm quite new with this so not really sure how to check that, I added NLog.WindowsEventLog using the nuget package manager. Is there anything else i need to do to get the application to pick up the dll and get it to work? Or should it work once you install the nuget and add the
Yea looking at the logs it says the following, then it fails on the next line. I'll attach my log.
Thanks
2018-08-02 09:23:49.5709 Info Message Template Auto Format enabled
2018-08-02 09:23:49.5709 Info Loading assembly: NLog.WindowsEventLog
2018-08-02 09:23:49.6411 Error Parsing configuration from C:\Users\daera-jonesj\source\repos\TestLogging\TestLogging\nlog.config failed. Exception: NLog.NLogConfigurationException: Exception when parsing C:\Users\daera-jonesj\source\repos\TestLogging\TestLogging\nlog.config. ---> System.ArgumentException: Target cannot be found: 'EventLog'
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
--- End of inner exception stack trace ---
2018-08-02 09:23:49.6411 Warn Failed loading config from C:\Users\daera-jonesj\source\repos\TestLogging\TestLogging\nlog.config. Invalid XML?
2018-08-02 09:23:49.6480 Info Found 0 configuration items
2018-08-02 09:23:52.0249 Info Shutting down logging...
2018-08-02 09:23:52.0487 Info Logger has been shut down.
strange, it does "Loading assembly: NLog.WindowsEventLog"
but also reports: "Target cannot be found: 'EventLog'"
Yea I've been so confused trying to get this to work. Does it work fine with console apps in .NET Core 2.1? Or do i need to add something else in the program code to start up or reference the NLog.WindowsEventLog assembly?
I'm currently dealing with the same problem. FYI
2018-08-02 15:06:34.7803 Info Loading assembly: NLog.Web.AspNetCore
2018-08-02 15:06:34.7803 Info Loading assembly: NLog.WindowsEventLog
2018-08-02 15:06:35.1757 Error Parsing configuration from C:\xxxx\bin\Debug\netcoreapp2.1\nlog.config failed. Exception: NLog.NLogConfigurationException: Exception when parsing C:\xxxxxx\bin\Debug\netcoreapp2.1\nlog.config. ---> System.ArgumentException: Target cannot be found: 'EventLog'
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
--- End of inner exception stack trace ---
2018-08-02 15:06:35.1831 Warn Failed loading config from C:\xxxxxx\Debug\netcoreapp2.1\nlog.config. Invalid XML?
2018-08-02 15:06:35.2027 Info Found 0 configuration items
```xml
autoReload="true"
internalLogLevel="info"
internalLogFile="c:\temp\internal-nlog.txt">
<target xsi:type="EventLog"
name="eventlog"
log ="xxx"
source="xxxx"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
Even tried to include the compability pack
```xml
<ItemGroup>
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.1" />
<PackageReference Include="NLog.WindowsEventLog" Version="4.5.7" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.0" />
</ItemGroup>
Rather than excluding the EventLogTarget by conditional debug statements, isn't it better to check at runtime if we're running on windows?
c#
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{}
https://github.com/dotnet/designs/blob/master/accepted/compat-pack/compat-pack.md
Or by checking if the following package is installed when running on .NET Core:
https://www.nuget.org/packages/System.Diagnostics.EventLog/
Rather than excluding the EventLogTarget by conditional debug statements, isn't it better to check at runtime if we're running on windows?
yes, we don;t have conditional debug statements for Windows?
I think I see the problem, working on it
If you like a pre-release, it could be downloaded here: https://ci.appveyor.com/project/nlog/nlog/build/4.5.0-8021/artifacts
PS: I'm thinking to re-version it to 1.0, any opinion on it? (I will also unlist older versions)
The package is now update every NLog release (so also if there are no changes), but i'm not sure if that is confusing. (ping @snakefoot )
@304NotModified Well nothing wrong with https://www.nuget.org/packages/NLog.WindowsEventLog/4.5.6 . It is just the ver. 4.5.7 that is not working (Because latest msbuild has changed something)
I would just unlist ver. 4.5.7 and get 4.5.8 out (Also need to fix NLog.WindowsIdentity)
@snakefoot @304NotModified I tried changing my nuget package to use the 4.5.6 and it works now. I'll keep an eye out for when the new version is released to nuget.
When is 4.5.8 expected to release?
this weekend
@snakefoot , @gingjonesy thx for feedback!
4.5.8 is now live!
Most helpful comment
I think I see the problem, working on it