Logging via log4net for TextWriter supplied at runtime

I have a message handler with the following signature working in Azure:

public static void DispatchQueueMessage([QueueTrigger("bq")] string msg, TextWriter tw) 

I want to configure log4net to use the provided TextWriter.

If this is not possible in the general case, I would completely agree to the conclusion of an ILog instance with standard formatting!


As a result, I did (something like) the following:

  ILog log = LogManager.GetLogger(name); var appender = new TextWriterAppender(); appender.Writer = tw; appender.Threshold = log4net.Core.Level.Debug; DynamicPatternLayout layout = new DynamicPatternLayout(); layout.ConversionPattern = "%d [%t] %-5p %m%n"; layout.ActivateOptions(); appender.Layout = layout; appender.ActivateOptions(); ((log4net.Repository.Hierarchy.Logger) log.Logger).AddAppender(appender); Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); hierarchy.Configured = true; return log; 

Is there a better way?

+6
source share

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


All Articles