在Excel Add-in中使用log4NET

来源:互联网 发布:淘宝禁售管理规范 编辑:程序博客网 时间:2024/06/05 22:37

在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    }}