Skip to content

NLog.Web Example Projects with better default NLog.config #1125

@snakefoot

Description

@snakefoot

The example NLog.config-xml-file tries to show many things at once. How to redirect output to multiple files, and capture different HttpContext-details. More a demonstration of what is possible, and inspiration for the user to think up their own perfect logging-configuration.

But instead of being a prototype/showcase/exercise of what is possible, then it might be better to provide NLog.config that works out of the box for 80 pct of users. Maybe something basic like this:

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      throwConfigExceptions="true">

  <extensions>
    <add assembly="NLog.Extensions.Logging" />
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>

  <targets async="true">
    <target name="logDebugger" xsi:type="Debugger" />

    <target name="logConsole" xsi:type="Console" layout="${MicrosoftConsoleLayout}" />

    <target name="logFile" xsi:type="File" fileName="log-${shortdate}.txt" maxArchiveFiles="30">
      <layout xsi:type="MicrosoftConsoleJsonLayout" includeScopes="true" includeActivityIds="true">
        <state name="url" layout="${aspnet-request-url}" />
        <state name="method" layout="${aspnet-request-method}" />
        <state name="statuscode" layout="${aspnet-response-statuscode}" />
        <state name="requestip" layout=" ${aspnet-request-ip} " />
        <state name="hostname" layout="${hostname}" />
        <state name="servicename" layout="${host-appname}" />
        <state name="environment" layout="${host-environment}" />
      </layout>
    </target>
  </targets>

  <rules>
    <logger name="System.*" finalMinLevel="Warn" />
    <logger name="Microsoft.*" finalMinLevel="Warn" />
    <logger name="Microsoft.Hosting.Lifetime*" finalMinLevel="Info" /> <!-- Overrides previous rule -->
    <logger name="*" minLevel="Info" writeTo="logDebugger" />
    <logger name="*" minLevel="Info" writeTo="logConsole" />
    <logger name="*" minLevel="Info" writeTo="logFile" />
  </rules>
</nlog>

See also: https://github.com/NLog/NLog/wiki/Tutorial

Maybe update example-project to also setup NLog-MiddleWare for logging http-request that are failing/slow?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions