Log Exclusion Properties

Is it possible to register exception properties with NLog?

For example, a SocketException has properties such as ErrorCode, HResult, NativeErrorCode, etc. that apply only to these exceptions. Is it possible to register them without explicitly logging (i.e., without using Log(e.ErrorCode)) them and using only ErrorException from the code? By default, the Exception Layout Display Tool simply calls ToString to throw an exception.

+3
source share
3 answers

I do not know if this is a very good idea, but you can write your own LayoutRenderer. To keep it simple, I only wrote one that inherits ExceptionLayoutRenderer and overrides 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);
            }
        }
    }

SocketException handling is not very complicated. I am sure there is a better way, but it shows how you can do it.

To activate this, you must configure your config as follows:

<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

Well, I did not know that you want this function for every exception that has its own properties. If you are only interested in a few others, you can simply add more if (the exception is your ExceptionType) and configure what properties you are interested in. A more general approach is to use reflection to register all properties defined for exclusion.

[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);
        }
    }

, .

+3

NLog, try/catch, , ToString ,

, , , ToString, .

ToString SocketException, , #.

+1

?

0

Source: https://habr.com/ru/post/1776275/


All Articles