收录日期:2020/06/02 03:47:36 时间:2011-02-26 11:59:19 标签:c#,log4net

I currently use log4net with a RollingFileAppender.

As each Log call is made I'd like to store this in memory. At the end of my console application run I'd like to (if an app.config setting is true) take only the Warns and Fatals and send all these messages in an Email. I notice MemoryAppender but not quite sure how to use it. Also see SMTPAppender but not sure it is the right tool, else I'll use MemoryAppender and somehow filter out only events of Levels Warn/Fatal and then email using the SmtpClient class.

How to achieve this?

Thanks

Update

My last part of log4net config now looks like.

<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender" >
    <onlyFixPartialEventData value="true" />
    <threshold value="WARN" />
  </appender>  

  <root>
     <level value="DEBUG" />
    <appender-ref ref="Console" />
    <appender-ref ref="RollingFile" />
    <appender-ref ref="MemoryAppender" />    
  </root>

In code I do:

private static MemoryAppender MemoryAppender
{
     get
     {
                if (memoryAppender == null)
                {
                    Hierarchy h = LogManager.GetRepository() as Hierarchy;
                    memoryAppender = h.Root.GetAppender("MemoryAppender") as MemoryAppender;
                }
                return memoryAppender;
            }
     }

Then when I want the events I call:

MemoryAppender.GetEvents();

I've tried MemoryAppender.GetEvents()[0].RenderedMessage but that is not the correct output, how do I get the message string as it was written to the File/Console logs with the correct pattern and time etc and build myself a StringBuilder? I'll then put this in the body of my Email and send it using the SmtpClient. RenderMessage is just giving me the string that was provided to the Log.Warn() call not what was written to the log. Is this due to not setting a layout pattern on the MemoryAppender?

Thanks

MemoryAppender will only "append" to memory and is thus mostly useful only for development and testing purposes. And there is currently no appender that will only append on application shutdown.

The SMTPAppender is something in between, since it inherits the BufferingAppenderSkeleton. These appenders have a BufferSize property which controls how many messages are kept in memory before they are flushed.

Which messages to pass to the appenders are controlled with the level settings either on the root element or on individual logger elements. In your case use a level of WARN which will let through WARN, ERROR and FATAL. If you don't want the ERROR messages you will have to put a level filter on your appender.

Update: MemoryAppender is not using any layout to "render" message objects. What you get from MemoryAppender is just the raw message objects as they are produced by log4net. You will have to convert those to meaningfull text yourself.

Alternatively, if you require both layout functionality and in-memory appending you could look into subclassing AppenderSkeleton. That way you get the basic Layout support. When implementing the Append method you can do what MemoryAppender does, that is just appending to an internal list of messages.

Update 2: to implement the MemoryAppender alternative I suggest taking the MemoryAppender as a starting point. MemoryAppender is a subclass of AppenderSkeleton and have thus access to the RenderLoggingEvent method. So, we subclass MemoryAppender and add a method that renders the current batch of logging events:

public class RenderingMemoryAppender : MemoryAppender
{

    public IEnumerable<string> GetRenderedEvents()
    {
        foreach(var loggingEvent in GetEvents())
        {
            yield return RenderLoggingEvent(loggingEvent);
        }
    }
}

You can use SMTPAppender and look at how the flush functionality works. Log4net keeps all messages in memory until flush is called (if it's setup this way), so the email will be sent when you flush it.

Another thing you can do is create a separate appender (Rolling or FileAppender) with filters WARN and FATAL, then attach this appender to the same logger, and at the end of your run email this file if it's non-empty (and you can choose to send it as an attachment or right in the body of the email). Let me know if you want more details, this is almost the same I'm doing know.

Good luck!

Ricardo.