Application Block Launch - Caller Registration

When logging into Log4Net, it is very easy to put the class that called the log into the log file. In the past, I have found that it is very easy to track code and view a stream through classes. In Log4Net, I use the% logger property in the conversion template, for example:

<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

And this gives me the result I want:

2008-09-19 15:40:26,906 [3132] ERROR <b>Log4NetTechDemo.Tester</b> [(null)] - Failed method

On output, you can see that the class that the log called is Log4NetTechDemo.Tester, so I can easily trace the error back to this class.

In the Appliance Logging block, I cannot figure out how to do this with a simple log call. Does anyone know how to do this? If so, then an example or steps for this would be very helpful.

+2
source share
2 answers

Add a call method to the LogEntry ExtendedProperties dictionary; unless you removed ExtendedProperties tokens from the formatting template, of course.

Put something like this in the journal wrappers:

public void LogSomething(string msg)
{
  LogEntry le = new LogEntry { Message = msg };
  le.ExtendedProperties.Add("Called from", new StackFrame(1).GetMethod().ReflectedType);
  Logger.Write(le);
}

The call causes the following at the end of the log:

Extended Properties: Called from - LAB_Demo.Tester
+5
source

We did not find an easy way without clicking StackTrace. If this is an exception, we just take from this:

 StackTrace trace = new StackTrace(ex, true);
 StackFrame frame = trace.GetFrame(0);

For chats, we just write a line. We have a fragment capable of capturing the class name when pasting. We also declare a const string with the class name.

Not beautiful, but this is the best we have found. I hope someone has the best answer in this thread :)

+1
source

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


All Articles