Note that there are some explanatory texts on larger screens.

plurals
  1. POc# log4net store entries in memory and Email under certain conditions
    text
    copied!<p>I currently use log4net with a RollingFileAppender.</p> <p>As each Log call is made I'd like to <strong>store this in memory</strong>. 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 <strong>MemoryAppender</strong> but not quite sure how to use it. Also see <strong>SMTPAppender</strong> 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.</p> <p>How to achieve this?</p> <p>Thanks</p> <p><strong>Update</strong></p> <p>My last part of log4net config now looks like.</p> <pre><code>&lt;appender name="MemoryAppender" type="log4net.Appender.MemoryAppender" &gt; &lt;onlyFixPartialEventData value="true" /&gt; &lt;threshold value="WARN" /&gt; &lt;/appender&gt; &lt;root&gt; &lt;level value="DEBUG" /&gt; &lt;appender-ref ref="Console" /&gt; &lt;appender-ref ref="RollingFile" /&gt; &lt;appender-ref ref="MemoryAppender" /&gt; &lt;/root&gt; </code></pre> <p>In code I do:</p> <pre><code>private static MemoryAppender MemoryAppender { get { if (memoryAppender == null) { Hierarchy h = LogManager.GetRepository() as Hierarchy; memoryAppender = h.Root.GetAppender("MemoryAppender") as MemoryAppender; } return memoryAppender; } } </code></pre> <p>Then when I want the events I call:</p> <p>MemoryAppender.GetEvents();</p> <p>I've tried MemoryAppender.GetEvents()[0].RenderedMessage but that is not the correct output, <strong>how do I get the message string as it was written to the File/Console logs with the correct pattern and time etc</strong> and build myself a StringBuilder? I'll then put this in the body of my Email and send it using the SmtpClient. <strong>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?</strong></p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload