2017-04-06 33 views
0

我有一个Visual Studio 2015扩展,并且想使用log4net。 但是,我没有收到任何日志消息,甚至没有收到来自log4net的内部调试消息。log4net不能与VS 2015扩展一起使用

在Package I类有:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
在初始化方法

,后来我有:

if (!log4net.LogManager.GetRepository().Configured) 
     { 
      log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("C:\\Program Files\\Sym\\bin\\Log4NetSettingsGlobal.xml")); 
     } 
     log.Debug("Package Initialize"); 

在VSX项目的app.config我有这样的:

<configuration> 
    <appSettings> 
    <add key="log4net.Internal.Debug" value="true"/> 
    </appSettings> 
    <system.diagnostics> 
    <trace autoflush="true"> 
     <listeners> 
     <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Program Files\Sym\Logging\Log4Net_Trace_VSX.txt"/> 
     </listeners> 
    </trace> 
    </system.diagnostics> 
... 
</configuration> 

没有写入上面的文本文件。当我在VS中调试扩展时,没有什么会写入输出窗口,也就是打开VS的第二个实例,以便测试扩展。

我也添加了一个DebugAppender from this SO question。仍然没有在输出窗口中。

我在做什么错?

回答

1

根据您的描述,我使用log4net创建了一个简单的自定义命令vsix项目,但不更改app.config文件,它可以工作。

Log4Net.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 
    </configSections> 
    <log4net> 
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 
     <!--log Path--> 
     <param name= "File" value= "F:\App_Log\"/> 
     <!--append log to file--> 
     <param name= "AppendToFile" value= "true"/> 
     <!--log keep days--> 
     <param name= "MaxSizeRollBackups" value= "10"/> 
     <param name= "StaticLogFileName" value= "false"/> 
     <!--log farmat:2008-08-31.log--> 
     <param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/> 
     <param name= "RollingStyle" value= "Date"/> 
     <layout type="log4net.Layout.PatternLayout"> 
     <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> 
     </layout> 
    </appender> 
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> 
     <mapping> 
     <level value="ERROR" /> 
     <foreColor value="Red, HighIntensity" /> 
     </mapping> 
     <mapping> 
     <level value="Info" /> 
     <foreColor value="Green" /> 
     </mapping> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" /> 
     </layout> 
     <filter type="log4net.Filter.LevelRangeFilter"> 
     <param name="LevelMin" value="Info" /> 
     <param name="LevelMax" value="Fatal" /> 
     </filter> 
    </appender> 
    <root> 
     <!--(high) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (low) --> 
     <level value="all" /> 
     <appender-ref ref="ColoredConsoleAppender"/> 
     <appender-ref ref="RollingLogFileAppender"/> 
    </root> 
    </log4net> 
</configuration> 

自定义命令与log4net的

using System; 
using System.ComponentModel.Design; 
using System.Globalization; 
using Microsoft.VisualStudio.Shell; 
using Microsoft.VisualStudio.Shell.Interop; 
using log4net.Config; 
using System.IO; 
using log4net; 

namespace CommandDemo 
{ 
    /// <summary> 
    /// Command handler 
    /// </summary> 
    internal sealed class FirstCommand 
    { 
     /// <summary> 
     /// Command ID. 
     /// </summary> 
     public const int CommandId = 0x0100; 

     /// <summary> 
     /// Command menu group (command set GUID). 
     /// </summary> 
     public static readonly Guid CommandSet = new Guid("30f5b2c4-9c7f-4748-a1de-d6d03fe40eda"); 

     /// <summary> 
     /// VS Package that provides this command, not null. 
     /// </summary> 
     private readonly Package package; 

     private ILog logger; 

     /// <summary> 
     /// Initializes a new instance of the <see cref="FirstCommand"/> class. 
     /// Adds our command handlers for menu (commands must exist in the command table file) 
     /// </summary> 
     /// <param name="package">Owner package, not null.</param> 
     private FirstCommand(Package package) 
     { 
      InitLog4Net(); 
      logger = LogManager.GetLogger(typeof(FirstCommand)); 

      if (package == null) 
      { 
       throw new ArgumentNullException("package"); 
      } 

      this.package = package; 

      OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService; 
      if (commandService != null) 
      { 
       var menuCommandID = new CommandID(CommandSet, CommandId); 
       var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID); 
       commandService.AddCommand(menuItem); 
      } 
     } 

     /// <summary> 
     /// Gets the instance of the command. 
     /// </summary> 
     public static FirstCommand Instance 
     { 
      get; 
      private set; 
     } 

     /// <summary> 
     /// Gets the service provider from the owner package. 
     /// </summary> 
     private IServiceProvider ServiceProvider 
     { 
      get 
      { 
       return this.package; 
      } 
     } 

     /// <summary> 
     /// Initializes the singleton instance of the command. 
     /// </summary> 
     /// <param name="package">Owner package, not null.</param> 
     public static void Initialize(Package package) 
     { 
      Instance = new FirstCommand(package); 
     } 

     /// <summary> 
     /// This function is the callback used to execute the command when the menu item is clicked. 
     /// See the constructor to see how the menu item is associated with this function using 
     /// OleMenuCommandService service and MenuCommand class. 
     /// </summary> 
     /// <param name="sender">Event sender.</param> 
     /// <param name="e">Event args.</param> 
     private void MenuItemCallback(object sender, EventArgs e) 
     { 
      string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName); 
      string title = "FirstCommand"; 

      logger.Debug("DebugInfo"); 
      //logger.Info("TestInfo"); 
      //logger.Warn("Testwarning"); 
      //logger.Error("Testexception"); 
      //logger.Fatal("Testerror"); 
      // Show a message box to prove we were here 
      VsShellUtilities.ShowMessageBox(
       this.ServiceProvider, 
       message, 
       title, 
       OLEMSGICON.OLEMSGICON_INFO, 
       OLEMSGBUTTON.OLEMSGBUTTON_OK, 
       OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); 
     } 


     private void InitLog4Net() 
     { 
      var logCfg = new FileInfo(@"C:\log4netconfigpath\log4net.config"); 
      XmlConfigurator.Configure(logCfg); 
     } 
    } 
} 
+0

我发现,我没有在bin目录中的配置文件,因为我写了一个新的安装程序,忘了添加它。无论如何,知道你可以让它工作,让我再次检查一切。谢谢,你帮了我! – Igavshne

相关问题