Smoother log4net integration in .Net applications

Part 1

Many developers I’ve seen and many blogs on internet use the following line to declare a Logger in a class:

private static readonly log4net.ILog Log = 
    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Although it might bring some advantages in some cases (inheritance), most of the time it’s not really necessary to obtain the surrounding Type by calling reflection, so I generally prefer to use:

private static readonly log4net.ILog Log = 
    log4net.LogManager.GetLogger(typeof(MyClass));

where MyClass is the class I’m adding this as a member;

This is a bit prone to Copy/Paste mistakes when this line is taken from one class and pasted in another, and you forget to change the name of the class. So, to overcome this, I’ve made Visual Studio code snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>log</Title>
      <Author>[USERNAME]</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>log</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>className</ID>
          <ToolTip>The full name of the type will be used as the name of the logger to retrieve.</ToolTip>
          <Default>
          </Default>
          <Function>ClassName()</Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Delimiter="$"><![CDATA[private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof($className$));]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Put this into a file named log.snippet and save it to your Documents\Visual Studio 2017\Code Snippets\Visual C#\My Code Snippets\ folder and when you will write log in Visual Studio and press TAB twice, the line will be inserted with the name of the class correctly updated.

 

Part 2

There is an online, unofficial but complete, schema that can be used to have IntelliSense when editing the log4net.config file. You can download it here: http://csharptest.net/downloads/schema/log4net.xsd.