Add logging to Microsoft Office add-ins

Microsoft Office 2020 m365 Excel_HD

In this post I explain how to add logging to Microsoft Office add-ins for debug also when your add-in is on the client machine.

I have written a simple Microsoft Excel AddIn in Visual Studio 2019 and I would like to be able to log events. I am looking for the simplest approach to enable logging events from my VSTO that does not violate good programming guidelines. Also, if users have issues, I want them to send me the logs. So, I can understand what happened.

Add log4net

So, it is relatively straightforward to setup log4net to handle simple (or complex) logging requirements.

  1. Install log4net to your project using the NuGet Package Manager or Package Manager Console if you prefer.
  2. Add an App.config file if you don’t have one already (right-click your project, select Add > New Item… and select Application Configuration File from Visual C# Items).
  3. Edit the App.config file to look something like this (replacing MyAddIn appropriately in the <file /> tag). Log files will be saved to the user’s roaming AppData (%appdata%) folder, keeping up to 5 rolling log files which do not exceed 5MB each.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" 
             requirePermission="false" />
  </configSections>
  <log4net>
    <root>
      <level value="ALL"/> 
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="${APPDATA}\My AddIn\MyAddin-log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>
  1. Add the following line to ThisAddIn_Startup() in ThisAddIn.cs (you will need to add a using log4net; directive):
log4net.Config.XmlConfigurator.Configure();
  1. Add the following statement to any class which needs to implement logging:
private static readonly log4net.ILog log = 
log4net.LogManager.GetLogger(System.Reflection.MethodBase 
       .GetCurrentMethod().DeclaringType);
  1. Events can then be logged using the log.Infolog.Warnlog.Errorlog.Fatal and other methods provided by log4net.

With log4net we can add logging to Microsoft Office add-ins and save the logs on a text file, in the Event Log or more options are available.

You may get schema warning messages because log4net does not include a schema, and Visual Studio cannot then validate the config section. In that case, download the schema file from https://csharptest.net/downloads/schema/log4net.xsd to somewhere accessible.

Select your App.config file in Visual Studio, and click into the editor. You should get an XML menu item. From there, select Schemas… and add the log4net.xsd file from where you saved it.

Note that you can also use log4net to log to the Event Viewer but you will run into the same permission issues raised in the initial question.

Conclusion

Finally, we know how to add logging to our Microsoft Office add-ins. If you have any question, please comment below or use our forum.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.