none
Creating log file with log4net for Word add-in RRS feed

  • Question

  • Hello,

    I have a Word add-in that I would like to create a log file for using log4net.

    Using the documentation online my code is:

    In App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
      </configSections>
      <applicationSettings>
            <setting name="DefaultFontName" serializeAs="String">
            <value>Calibri</value>
          </setting>

          <log4net>
            <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
              <file value="c:\Users\luke\AppData\Local\Temp\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>

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

    </applicationSettings>

    </configuration>

    In the class I want to use the log:

    using log4net;
    using log4net.Config;

    namespace Template.Controls
    {
        public partial class PaneControl : UserControl
        {

      Load += OnTrexPaneControlLoad;

    // Ive tried these three:

     

    //private readonly ILog log = LogManager.GetLogger("TrexPaneControl.cs");
            // private static readonly ILog log = LogManager.GetLogger(typeof(TrexPaneControl));
            //private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        }

    The event/function that I want to write to the log file:

    void OnPaneControlLoad(object sender, EventArgs e)
    {

        log4net.Config.XmlConfigurator.Configure();
        BasicConfigurator.Configure();
        log.Info("Entering application.");

    }

    }

    Ive also tried putting [assembly: log4net.Config.XmlConfigurator(Watch = true)] in the AssemblyInfo.cs file as well, but have yet to be able to write to the log. I am pretty sure I have full permission to write to this folder.

    Any help would be greatly appreciated!

    Wednesday, November 18, 2015 6:42 PM

Answers

  • Hi Luke415,

    I made a test with the link below, and it could create the log file correctly. I suggest you do as the steps.

    # How to use Apache log4net library with ASP.NET MVC 5 for logging.
    https://code.msdn.microsoft.com/How-to-use-Apache-log4net-0d969339

    Since the above link is for Asp.net, you could change the steps which are not the same in VSTO.

    1. After you add the log4net reference, add the code below to your ThisAddin.cs.

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "App.config", Watch = true)]  

    Something like:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "App.config", Watch = true)]   
    namespace WordAddIn
    {
        public partial class ThisAddIn
        {

    2. Add a app.config to VSTO project, all the app.config looks like below, pay attention to the file value, I suggest you try other file path instead of using file in Temp to check whether it would work:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <!-- Add log4net config section-->
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,  log4net" />
      </configSections>
    
      <log4net debug="true">
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="D:\logs\log.txt" />
          <appendToFile value="true" />
          <rollingStyle value="Size" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="10MB" />
          <staticLogFileName value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
          </layout>
        </appender>
    
        <root>
          <level value="DEBUG" />
          <appender-ref ref="RollingLogFileAppender" />
        </root>
      </log4net>
    </configuration>
    

    3. Add the code below in ThisAddIn_Startup method, pay attention to the file path of App.config

    log4net.Config.XmlConfigurator.Configure(new FileInfo(@"D:\Edward\WordAddIn\App.config"));

    4. Then you could use log4net as below:

            readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            private void LogBtn_Click(object sender, RibbonControlEventArgs e)
            {
                MessageBox.Show("ok");
                logger.Info("Hello World");
            }

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Luke415 Thursday, November 19, 2015 4:35 PM
    Thursday, November 19, 2015 5:47 AM

All replies

  • Hi Luke415,

    I made a test with the link below, and it could create the log file correctly. I suggest you do as the steps.

    # How to use Apache log4net library with ASP.NET MVC 5 for logging.
    https://code.msdn.microsoft.com/How-to-use-Apache-log4net-0d969339

    Since the above link is for Asp.net, you could change the steps which are not the same in VSTO.

    1. After you add the log4net reference, add the code below to your ThisAddin.cs.

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "App.config", Watch = true)]  

    Something like:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "App.config", Watch = true)]   
    namespace WordAddIn
    {
        public partial class ThisAddIn
        {

    2. Add a app.config to VSTO project, all the app.config looks like below, pay attention to the file value, I suggest you try other file path instead of using file in Temp to check whether it would work:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <!-- Add log4net config section-->
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,  log4net" />
      </configSections>
    
      <log4net debug="true">
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="D:\logs\log.txt" />
          <appendToFile value="true" />
          <rollingStyle value="Size" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="10MB" />
          <staticLogFileName value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
          </layout>
        </appender>
    
        <root>
          <level value="DEBUG" />
          <appender-ref ref="RollingLogFileAppender" />
        </root>
      </log4net>
    </configuration>
    

    3. Add the code below in ThisAddIn_Startup method, pay attention to the file path of App.config

    log4net.Config.XmlConfigurator.Configure(new FileInfo(@"D:\Edward\WordAddIn\App.config"));

    4. Then you could use log4net as below:

            readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            private void LogBtn_Click(object sender, RibbonControlEventArgs e)
            {
                MessageBox.Show("ok");
                logger.Info("Hello World");
            }

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Luke415 Thursday, November 19, 2015 4:35 PM
    Thursday, November 19, 2015 5:47 AM
  • Thank you!
    Thursday, November 19, 2015 4:35 PM
  • Hi Im realizing there is another issue. I am trying to deploy this to test users and they do not get the logs generated. I am installing the .exes myself and also do not get the logs generated. Is there a property that needs to change in Visual Studio for this to work from a .exe install?

    Thursday, December 10, 2015 2:42 PM
  • Hi Luke415,

    For this new issue, I suggest you post a new thread, and then other community members would help you.
    In addition, it would be helpful if you could share us how you deploy your project and whether you got any error.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, December 11, 2015 3:02 AM
  • Hi Luke415,

    i want to create log file for MS-Word.

    please guide me how to create and i am new to this!!!!

    Thursday, May 19, 2016 7:30 AM
  • Hi yangyeswar,

    I suggest you refer my first reply for how to use log file. If you still have any issues, I suggest you post a new thread, and then more community members could help you.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Proposed as answer by yangyeswar Friday, May 20, 2016 10:46 AM
    Friday, May 20, 2016 6:01 AM