NLog custom layout renderer for "EventProperties" is not working

40 Views Asked by At

I have implemented a NLog in asp.net core MVC application. I read the documentation of the NLog file and did the code as they gave it, and it worked for default layout. But when I add a custom layout with event properties, the code is not working as per my custom layout. It just prints the date, time, logger name, exception message, and exception (if it came). But when I modify to print the URL, action, and event properties, it does not work. Where am I doing wrong? Please assist me. My 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"
      internalLogLevel="Info"
      internalLogFile="${aspnet-webrootpath}/logs/logsinternal-nlog-AspNetCore.txt">

    <!-- enable asp.net core layout renderers -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>

    <!-- the targets to write to -->
    <targets>
        <!-- File Target for all log messages with basic details -->
        <target xsi:type="File" name="allfile" fileName="${aspnet-webrootpath}/logs/nlog-AspNetCore-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|Exception-data: ${exceptiondata:item=String:format=String:BaseException=Boolean}|" />

        <!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
        <target xsi:type="File" name="ownFile-web" fileName="${aspnet-webrootpath}/logs/nlog-AspNetCore-own-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}| Exception Message: ${message} ${exception:format=tostring}| ActionResult: ${event-properties:item=ActionResult:culture=String:format=String:objectpath=String}|InputParameters: ${event-properties:item=InputParameters:jsonEncode=true} |url: ${aspnet-request-url}|action: ${aspnet-mvc-action}| Exception Type: ${exception:format=Type}| Exception Trace: ${exception:format=StackTrace} |Exception-data: ${exceptiondata:item=String:format=String:BaseException=Boolean}}" />

        <!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
        <target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />
    </targets>

    <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />

        <!--Output hosting lifetime messages to console target for faster startup detection -->
        <logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />

        <!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
        <logger name="Microsoft.*" maxlevel="Info" final="true" />
        <logger name="System.Net.Http.*" maxlevel="Info" final="true" />

        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    </rules>
</nlog>

In appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

In controller class of action create and edit

Logger logger = LogManager.GetCurrentClassLogger();
LogEventInfo theEvent = new LogEventInfo(NLog.LogLevel.Debug, null, "Action Successfully Executed");
theEvent.Properties["InputParameters"] = jsonFile;
theEvent.Properties["ActionResult"] = "Party successfully created.";
logger.Log(theEvent);
0

There are 0 best solutions below