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 3438568
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:12:13+00:00 2026-05-18T08:12:13+00:00

Is it possible to log properties of exception with NLog? For example SocketException has

  • 0

Is it possible to log properties of exception with NLog?

For example SocketException has properties such as ErrorCode, HResult, NativeErrorCode, etc that are specific to only this type of exceptions. It is possible to log them without explicitly logging (i.e without using Log(e.ErrorCode)) them and using just ErrorException from the code? The default Exception layout renderer just calls ToString on the exception.

  • 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-18T08:12:13+00:00Added an answer on May 18, 2026 at 8:12 am

    I don’t know if it’s a very good idea, but you can write your own LayoutRenderer. To keep it simple I just wrote one that inherits from ExceptionLayoutRenderer and overrode the Append method.

    [LayoutRenderer("ExtendedException")]
        public class ExtendedExceptionLayoutRenderer : ExceptionLayoutRenderer
        {
            protected override void Append(System.Text.StringBuilder builder, LogEventInfo logEvent)
            {
                base.Append(builder, logEvent);
    
                var exception = logEvent.Exception;
                if (exception is SocketException)
                {
                    var sockException = (SocketException) exception;
                    builder.Append(sockException.ErrorCode).Append(" ").Append(sockException.SocketErrorCode);
                }
            }
        }
    

    The handling of the SocketException is not very sophisticated. I’m sure there is a better way, but it shows how you can do it.

    To activate that you have to adjust your config like this:

    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <extensions>
            <add assemblyFile="YourAssembly.dll"/>
        </extensions>
    
        <targets>
            <target name="console" xsi:type="Console" layout="${extendedexception} ${message}"/>
        </targets>
    
        <rules>
            <logger name="*" minlevel="Debug" writeTo="console" />
        </rules>
    </nlog>
    

    Edit

    Ok, I wasn’t aware that you want that feature for every exception that has it’s own properties. If there are only a few others you’re interested in, you can just add more if(exception is YourExceptionType) and adjust what properties you’re interested in.
    A more generic approach is to use reflection to log all properties that are defined on the exception.

    [LayoutRenderer("ExtendedException")]
        public class ExtendedExceptionLayoutRenderer : ExceptionLayoutRenderer
        {
            protected override void Append(System.Text.StringBuilder builder, LogEventInfo logEvent)
            {
                var exception = logEvent.Exception;
                var type = exception.GetType();
                var properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
    
                var logEntries = new SortedDictionary<string, string>();
    
                foreach (var property in properties)
                {
                    var name = property.Name;
                    var value = property.GetValue(exception, null).ToString();
                    logEntries.Add(name, value);
                }
    
                foreach (var entry in logEntries)
                {
                    builder.AppendFormat("{0}: {1} ", entry.Key, entry.Value);
                }
    
                base.Append(builder, logEvent);
            }
        }
    

    This adds every property that is declared on the exception type in alphabetical order to the log output.

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

Sidebar

Related Questions

Is it possible to log certain events to -> event log for example, while
How do I make Log4net only log Info level logs? Is that even possible?
Is it possible to access object properties that can only be accessed with the
Is it possible to only let a user log in while using a specific
Is it possible to log out user from a web site if he is
What are the possible causes of premature redo log switching in Oracle other than
I am using log.properties to configure logging for a Java app. Each class gets
Is it at all possible to use variable names in object literal properties for
Is it possible to run a script that gives the directory and files for
Working on a project that parses a log of events, and then updates a

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.