Programmatically force a new log file using Log4Net

NOTE. I read this question and answer and it does not work for what I want: Log4Net: programmatically specify multiple registrars (with multiple file applications)

I have a WCF service, which is a Q & A style service. It receives inputs and sends outputs. It's not that much.

I need to write each Q&A session to a separate file.

I have one Appender (currently RollingAppender).

Is there a way to run a new log file for every call to my WCF service?

NOTE. I use an XML layout, the idea is that the log output can be parsed and displayed graphically (a later function). A view like the Query Plan. This is another reason I need them in a separate file.

NOTE. If another reason is needed, Log4Net XmlLayoutBase will not drop the xml footers until the application closes. This is not a scheduled event for a WCF hosted in IIS.

+6
source share
3 answers

Instead of entering the file, perhaps you can try to enter the database table and register a session identifier with the registered data. Thus, you can make selections on the table based on the session identifier and see only their data.

+2
source

This seems to work for me:

public static void StartNewFile(this ILog log, string newFileName) { Logger logger = (Logger) log.Logger; while (logger != null) { foreach (IAppender appender in logger.Appenders) { FileAppender fileAppender = appender as FileAppender; if (fileAppender != null) { fileAppender.File = newFileName; fileAppender.ActivateOptions(); } } logger = logger.Parent; } } 

This requires the following links:

 using log4net; using log4net.Appender; using log4net.Repository.Hierarchy; 
+4
source

It may not be the exact solution you are looking for, but you can create a different logger for each session by calling it at the beginning of each interface call:

ILog logger = LogManager.GetLogger(<SessionID>);

You will get all the entries in the same log file, but then it is very simple to view each session separately using a viewer, for example log4view .

Hope this helps

0
source

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