Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:59:38+00:00 2026-05-20T09:59:38+00:00

NLog allows me to use SplitGroup to log my messages to several targets. I’d

  • 0

NLog allows me to use SplitGroup to log my messages to several targets. I’d like to use this feature to log each message to a common, user-specific and date-specific logs at once:

<variable name="commonLog" value="${logDir}\Common.log" />
<variable name="username" value="${identity:fSNormalize=true:authType=false:isAuthenticated=false}" />
<variable name="userLog" value="${logDir}\ByUser\${username}.log" />
<variable name="dateLog" value="${logDir}\ByDate\${shortdate}.log" />

<target name="logFiles" xsi:type="SplitGroup">
  <target xsi:type="File" fileName="${commonLog}" layout="${myLayout}" />
  <target xsi:type="File" fileName="${userLog}" layout="${myLayout}" />
  <target xsi:type="File" fileName="${dateLog}" layout="${myLayout}" />
</target>

This is great, but I also want to use different layouts for different levels of severity. For example, errorLayout would include exception information and insert [!] marker so I could later highlight errors in log viewers like BareTail:

<variable name="stamp" value="${date} ${username} ${logger}" />

<variable name="debugLayout" value="${stamp} ... ${message}" />
<variable name="infoLayout" value="${stamp} [i] ${message}" /> 
<variable name="warnLayout" value="${stamp} [!] ${message}" />
<variable name="errorLayout"
   value="${warnLayout}${newline}${pad:padding=10:inner=${exception:format=ToString}}" />

<!-- logFiles target -->

<rules>
  <logger name="*" level="Debug" writeTo="logFiles" layout="debugLayout"  />
  <logger name="*" level="Info" writeTo="logFiles" layout="infoLayout" />
  <logger name="*" level="Warn" writeTo="logFiles" layout="warnLayout" />
  <logger name="*" level="Error" writeTo="logFiles" layout="errorLayout" />
</rules>

This code assumes Errors always come with exceptions and Warnings don’t but that’s not the point.

The problem is this configuration is wrong. It won’t work because logger does not have layout attribute. It’s defined for target only.

Layout which is being used must be declared by targets themselves but I see no means of specifying different layouts for different severity levels.

For now, I had to copy-paste the same configuration code four times just to have four different layouts for same set of files:

<targets>
  <target name="logFilesDebug" xsi:type="SplitGroup">
    <target xsi:type="File" fileName="${commonLog}" layout="${debugLayout}" />
    <target xsi:type="File" fileName="${userLog}" layout="${debugLayout}" />
    <target xsi:type="File" fileName="${dateLog}" layout="${debugLayout}" />
  </target>

  <target name="logFilesInfo" xsi:type="SplitGroup">
    <target xsi:type="File" fileName="${commonLog}" layout="${infoLayout}" />
    <target xsi:type="File" fileName="${userLog}" layout="${infoLayout}" />
    <target xsi:type="File" fileName="${dateLog}" layout="${infoLayout}" />
  </target>

  <target name="logFilesWarn" xsi:type="SplitGroup">
    <target xsi:type="File" fileName="${commonLog}" layout="${warnLayout}" />
    <target xsi:type="File" fileName="${userLog}" layout="${warnLayout}" />
    <target xsi:type="File" fileName="${dateLog}" layout="${warnLayout}" />
  </target>

  <target name="logFilesError" xsi:type="SplitGroup">
    <target xsi:type="File" fileName="${commonLog}" layout="${errorLayout}" />
    <target xsi:type="File" fileName="${userLog}" layout="${errorLayout}" />
    <target xsi:type="File" fileName="${dateLog}" layout="${errorLayout}" />
  </target>
</targets>

<rules>
  <logger name="*" level="Debug" writeTo="logFilesDebug"  />
  <logger name="*" level="Info" writeTo="logFilesInfo" />
  <logger name="*" level="Warn" writeTo="logFilesWarn" />
  <logger name="*" level="Error" writeTo="logFilesError" />
</rules>

This just hurts my eyes.
Is there any better way to do this and avoid duplication?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:59:39+00:00Added an answer on May 20, 2026 at 9:59 am

    I’m not sure, but I think that you are probably stuck with the duplication. You want 4 different layouts to be used on the same file and you want 3 different files. One target requires one Layout. So, if you only wanted to log to 1 file, you would still have to define 4 Targets, each pointing to the same file and each with its own Layout. I don’t think that NLog has a more convenient way to associate multiple Layouts with a Target and then choosing one Layout based on the content of the logging message.

    Depending on exactly what you want to achieve with your formats, you might be able to reduce the duplication somewhat by writing a custom LayoutRenderer. In your example, you show that the Debug layout has “…” in it, Info has [i], Warn has [!], and Error has Warn + exception. You could write a LayoutRenderer that adds the special marker, depending on what the level of the message is. That way, you would roll Debug, Info, and Warn all into one Layout and Error would retain its own Layout.

    For example:

    Something like this for a custom LayoutRenderer (based on NLog 1.0 refresh, not 2.0):

      [LayoutRenderer("LevelMarkerLayoutRenderer")]   
      class LevelMarkerLayoutRenderer : LayoutRenderer   
      {     
        int estimatedSize = 3;      
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {       
          string marker;
          switch (logEvent.Level)
          {
            case Debug:
              marker = "...";
              break;
            case Info:
              marker = "[i]";
              break;
            case Warn:
              marker = "[!]";
              break;
            case Error:
              marker = "[!]";
              break;
            case Fatal:
              marker = "[!]";
              break;
            default:
              marker = "?";
          }
    
          builder.Append(marker);     
        }      
    
        protected override int GetEstimatedBufferSize(LogEventInfo logEvent)     
        {       
          return estimatedSize;     
        }
      } 
    

    Now you could configure two Layouts: “normal”, and “error”.

    Something like:

    <variable name="stamp" value="${date} ${username} ${logger}" />
    
    <variable name="normal" value="${stamp} ${LevelMarkerLayoutRenderer} ${message}" />
    <variable name="error"
       value="${warnLayout}${newline}${pad:padding=10:inner=${exception:format=ToString}}" />
    

    You could probably even create a custom LayoutRenderer to handle exceptions. If no exception, don’t output anything. If exception, concatentate newline, padding, and the exception string.

    If you had a “conditional” exception layout renderer, then you could have just one layout that might look like this:

    <variable name="normal" value="${stamp} ${LevelMarkerLayoutRenderer} ${message} ${ConditionalExceptionLayoutRenderer}" />
    

    Most of the time, ConditionalExceptionLayoutRenderer would yield null because there would not be an exception.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My NLog targets is like this: <targets> <target xsi:type=Console name=console layout=${longdate}|${level}|${message} /> <target xsi:type=File
This I think is related to my use of the nlog C++ API (and
Our team would like to use NLog for our logging needs - it does
NLog has an internal log that the developer can use to troubleshoot issues with
I'm using NLog like this try { // ... some code } catch(AException ex)
I have problem with this method in NLog library: NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(object state) It consume too
Does NLog have any sort of functionality to consolidate repetitive log messages when logging
Hi i try use Nlog in with caliburn micro, I have use this tutorial
I have a windows service and use nlog for logging. Everything works fine when
I would like to be able to pass some context info between NLog loggers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.