在Excel Add-in中使用log4NET

在Excel Add-in中没有办法设定配置文件。所以我们需要在程序中设定Log4NET的配置。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using log4net;
using System.Diagnostics;
using System.IO;


namespace ExcelAddIn4
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            try
            {
                log4net.Config.BasicConfigurator.Configure(new log4net.Appender.FileAppender(
     new log4net.Layout.PatternLayout("%d [%t]%-5p %c [%x] - %m%n"), @"C:\******\testfile.log")); 

                s.Close();
                ILog log = LogManager.GetLogger(typeof(ExcelAddIn4.Globals));
                log.Error("dddddddd");
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}



posted @ 2012-02-14 16:23  许阳 无锡  阅读(222)  评论(0编辑  收藏  举报