I'm currently trying to log the exception message, etc using NLog, no luck so far, and I can't imagine why
To setup NLog I used this guide: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(project.json)
I've setup this on the asp.net core, and tried the logging on my bussiness project
Bussiness
using Microsoft.Extensions.Logging;
public class FileService
{
private readonly ILogger<FileService> _logger;
public Task<DataResult<ICollection<BlankDTO>>> Test(Guid appId, Guid fileId)
{
try
{
throw new DivideByZeroException();
}
catch (DivideByZeroException ex)
{
_logger.LogError("EventId", ex, "Welp, something went wrong", new object[0]);
}
}
}
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="Warn"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- Load the ASP.NET Core plugin -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log">
<layout xsi:type="JsonLayout">
<attribute name="level" layout="${level}" />
<attribute name="timestamp" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}" />
<attribute name="systemUser" layout="${windows-identity:userName=true:domain=true}" />
<attribute name="currentUser" layout="${aspnet-user-identity}" />
<attribute name="machineName" layout="${machinename}" />
<attribute name="environment" layout="#{Environment}" />
<attribute name="applicationName" layout="My app" />
<attribute name="module" layout="My module" />
<attribute name="threadId" layout="${threadid}" />
<attribute name="message" layout="${event-properties:item=message}" />
<attribute name="messageDetails" layout="${exception}" />
<attribute name="variables" layout="${event-properties:item=Variables}" />
</layout>
</target>
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
This is all i get logged:
{ "level": "Error", "timestamp": "2017-06-20 09:51:24.904", "systemUser": "JONDOE-ORG\\foobar", "machineName": "BUC-007", "environment": "#{Environment}", "applicationName": "My app", "module": "My module", "threadId": "4" }
- NLog.Web.ASpNetCore (4.4.1)
- Microsoft.NETCore.App (1.1.2)