c a n d l a n d . n e t

Testing Log4Net Output

Dusty Candland | |

We have a custom logging framework setup which is using log4net under the covers. When doing that the ILogger should be used, not the ILog. The ILogger sets up the correct Class information.

Below is some code for testing the output from log4net, this code is just using log4net to write the message, which is not valuable as a test, but when the logging call is replaced with a custom framework you can make sure you’re getting what you expect in your log events.

private MemoryAppender appender;

<p style="margin: 0px">
  <span style="color: blue">private</span> <span style="color: #2b91af">LoggingEvent</span> </em>event;
</p>

<p style="margin: 0px">
  &#160;
</p>

<p style="margin: 0px">
  [<span style="color: #2b91af">SetUp</span>]
</p>

<p style="margin: 0px">
  <span style="color: blue">public</span> <span style="color: blue">void</span> Context()
</p>

<p style="margin: 0px">
  {
</p>

<p style="margin: 0px">
  &#160;&#160;&#160; <em>appender.Clear();</p> 
  
  <p style="margin: 0px">
    &#160;&#160;&#160; <span style="color: green">// Using log4net here, but should be replace with a custom logging class</span>
  </p>
  
  <p style="margin: 0px">
    &#160;&#160;&#160; <span style="color: green">// which calls ILogger.Log under it.</span>
  </p>
  
  <p style="margin: 0px">
    &#160;&#160;&#160; <span style="color: #2b91af">LogManager</span>.GetLogger(GetType()).Warn(<span style="color: #a31515">"my test desc."</span>, <span style="color: blue">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #a31515">"my test exception."</span>));
  </p>
  
  <p style="margin: 0px">
    &#160;&#160;&#160; </em>event = <em>appender.GetEvents()[0];</p> 
    
    <p style="margin: 0px">
      }
    </p>
    
    <p style="margin: 0px">
      &#160;
    </p>
    
    <p style="margin: 0px">
      [<span style="color: #2b91af">FixtureSetUp</span>]
    </p>
    
    <p style="margin: 0px">
      <span style="color: blue">public</span> <span style="color: blue">void</span> SetupLog4NetMemoryAppender()
    </p>
    
    <p style="margin: 0px">
      {
    </p>
    
    <p style="margin: 0px">
      &#160;&#160;&#160; </em>appender = <span style="color: blue">new</span> <span style="color: #2b91af">MemoryAppender</span>();
    </p>
    
    <p style="margin: 0px">
      &#160;&#160;&#160; <em>appender.Name = <span style="color: #a31515">"Unit Testing Appender"</span>;</p> 
      
      <p style="margin: 0px">
        &#160;&#160;&#160; </em>appender.Layout = <span style="color: blue">new</span> log4net.Layout.<span style="color: #2b91af">PatternLayout</span>(<span style="color: #a31515">"%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n"</span>);
      </p>
      
      <p style="margin: 0px">
        &#160;&#160;&#160; <em>appender.Threshold = <span style="color: #2b91af">Level</span>.All;</p> 
        
        <p style="margin: 0px">
          &#160;&#160;&#160; </em>appender.ActivateOptions();
        </p>
        
        <p style="margin: 0px">
          &#160;
        </p>
        
        <p style="margin: 0px">
          &#160;&#160;&#160; <span style="color: #2b91af">Logger</span> root = ((<span style="color: #2b91af">Hierarchy</span>) <span style="color: #2b91af">LogManager</span>.GetRepository()).Root;
        </p>
        
        <p style="margin: 0px">
          &#160;&#160;&#160; root.AddAppender(<em>appender);</p> 
          
          <p style="margin: 0px">
            &#160;&#160;&#160; root.Repository.Configured = <span style="color: blue">true</span>;
          </p>
          
          <p style="margin: 0px">
            }
          </p>
          
          <p style="margin: 0px">
            &#160;
          </p>
          
          <p style="margin: 0px">
            [<span style="color: #2b91af">FixtureTearDown</span>]
          </p>
          
          <p style="margin: 0px">
            <span style="color: blue">public</span> <span style="color: blue">void</span> RemoveLog4NetMemoryAppender()
          </p>
          
          <p style="margin: 0px">
            {
          </p>
          
          <p style="margin: 0px">
            &#160;&#160;&#160; <span style="color: #2b91af">Logger</span> root = ((<span style="color: #2b91af">Hierarchy</span>)<span style="color: #2b91af">LogManager</span>.GetRepository()).Root;
          </p>
          
          <p style="margin: 0px">
            &#160;&#160;&#160; root.RemoveAppender(</em>appender);
          </p>
          
          <p style="margin: 0px">
            &#160;&#160;&#160; root.Repository.Configured = <span style="color: blue">true</span>;
          </p>
          
          <p style="margin: 0px">
            }
          </p>
          
          <p style="margin: 0px">
            &#160;
          </p>
          
          <p style="margin: 0px">
            [<span style="color: #2b91af">Test</span>]
          </p>
          
          <p style="margin: 0px">
            <span style="color: blue">public</span> <span style="color: blue">void</span> Should_have_When_logging_type_in_log()
          </p>
          
          <p style="margin: 0px">
            {
          </p>
          
          <p style="margin: 0px">
            &#160;&#160;&#160; <span style="color: blue">var</span> data = _event.GetLoggingEventData();
          </p>
          
          <p style="margin: 0px">
            &#160;&#160;&#160; <span style="color: blue">var</span> info = data.LocationInfo;
          </p>
          
          <p style="margin: 0px">
            &#160;&#160;&#160; info.ClassName.ShouldEqual(<span style="color: #a31515">"Core.Tests.Instrumentation.When_logging_with_CustomLogger"</span>);
          </p>
          
          <p style="margin: 0px">
            &#160;&#160;&#160; info.MethodName.ShouldEqual(<span style="color: #a31515">"Context"</span>);
          </p>
          
          <p style="margin: 0px">
            }
          </p></p></div> 
          
          <p>
            BTW, the code highlighting was done using the Copy As HTML plug in from <a href="http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/">here</a>.
          </p></p>

Webmentions

These are webmentions via the IndieWeb and webmention.io. Mention this post from your site:


© 2006 - 2023 candland.net