Log4Net适用于VisualStudio调试/发布,但不适用于部署

问题描述:

为了制作更复杂的日志,我决定将Log4Net用于我的WPF桌面应用程序。它完美的工作,在'输出'创建'调试'消息,并写入文件的所有日志...Log4Net适用于VisualStudio调试/发布,但不适用于部署

但如果我部署它/安装它(使用Visual Studio的向导来创建.MSI设置)它停止工作。我无法在文件夹或与其相关的计算机上的任何位置找到任何文件。

我正在使用Caliburn.Micro来组织我的代码。为了获得日志,我使用一个名为“Logging.cs”的公共静态类来“集中”它。

“Initialize()”中的代码现在作为尝试的方式,但它似乎没有做任何事情。

public static class Logging 
{ 

    //Declaration of the logger 
    /// <summary> 
    /// Logger for the Logging manager. 
    /// </summary> 
    private static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 


    public static void Initialize() 
    { 
     log4net.Config.XmlConfigurator.Configure(); 
    } 
    /// <summary> 
    /// Creates a log in Fatal level accompanied by an expection 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="error">Exception thrown along with the log</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Fatal(string message, Exception error, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Fatal(message, error); 
    } 
    /// <summary> 
    /// Creates a log in Fatal level 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Fatal(string message, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Fatal(message); 
    } 

    /// <summary> 
    /// Creates a log in Error level accompanied by an expection 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="error">Exception thrown along with the log</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Error(string message, Exception error, Type senderType) 
    { 
     //Defines 'type' in the log. 
     MessageBox.Show(error.ToString()); 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Error(message, error); 
    } 
    /// <summary> 
    /// Creates a log in Error level 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Error(string message, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Error(message); 
    } 

    /// <summary> 
    /// Creates a log in Warn level accompanied by an expection 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="error">Exception thrown along with the log</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Warn(string message, Exception error, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Warn(message, error); 
    } 
    /// <summary> 
    /// Creates a log in Warn level 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Warn(string message, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Warn(message); 

    } 

    /// <summary> 
    /// Creates a log in Info level accompanied by an expection 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="error">Exception thrown along with the log</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Info(string message, Exception error, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Info(message, error); 
    } 
    /// <summary> 
    /// Creates a log in Info level 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Info(string message, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Info(message); 
    } 
    /// <summary> 
    /// Creates a log in Debug level accompanied by an expection 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="error">Exception thrown along with the log</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Debug(string message, Exception error, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Debug(message, error); 
    } 
    /// <summary> 
    /// Creates a log in Debug level 
    /// </summary> 
    /// <param name="message">The message to be logged</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void Debug(string message, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Debug(message); 
    } 
    /// <summary> 
    /// Log created in Debug level containing not only a log message but also an object to be serialized and logged. 
    /// </summary> 
    /// <param name="message">The message to be logged (should be description of object)</param> 
    /// <param name="obj">Object to be displayed</param> 
    /// <param name="senderType">From where the log comes from</param> 
    public static void ShowObject(string message, object obj, Type senderType) 
    { 
     //Defines 'type' in the log. 
     Logger = log4net.LogManager.GetLogger(senderType); 
     Logger.Debug(message + "\n<object>" + JsonConvert.SerializeObject(obj) + "</object>\n"); 
    } 
} 

而且,这里是我的log4net的配置文件:

<log4net> 
    <root> 
     <level value="ALL" /> 
     <appender-ref ref="console" /> 
     <appender-ref ref="file" /> 
    </root> 
    <appender name="console" type="log4net.Appender.ColoredConsoleAppender"> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date %level %logger - [%message] // %exception%newline" /> 
     </layout> 
    </appender> 
    <appender name="file" type="log4net.Appender.RollingFileAppender"> 
     <file type="log4net.Util.PatternString" value="DesktopApp.log" /> 
     <appendToFile value="true" /> 
     <rollingStyle value="Size" /> 
     <maxSizeRollBackups value="5" /> 
     <maximumFileSize value="10MB" /> 
     <staticLogFileName value="true" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date [%thread] %level %logger - [%message] // %exception%newline" /> 
     </layout> 
    </appender> 
<appender name="FileAppender" type="log4net.Appender.FileAppender"> 
    <file value="log-file.txt" /> 
    <appendToFile value="true" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 
    </layout> 
</appender> 
    </log4net> 

,并在AssemblyInfo.cs中,我只是在结尾插入这样的:

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

谢谢你你的时间,请评论,如果我可以添加任何更多的细节。

P.S .:软件安装在AppData中,所以不会出现文件权限问题,因为我正在使用另一个框架来自动更新应用程序。

P.S.2:我用的DebugView和主要信息的,似乎随机中间,我发现了一个错误:

[5480] log4net:ERROR Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> 

不过没有试过修复它,只是保持这个问题是最新的。 (谢谢Varun!)

+0

是否有原因DesktopApp.log无法在目标机器上创建? –

+0

该软件安装在Appdata的本地文件夹中,因此不会出现任何权限/文件写入问题。我以前使用的是“手动”日志,并且工作顺利。 – ironmagician

+0

您可以使用log4net调试内部发生的事情。它可能会给您一些洞察信息请点击此链接 - https://logging.apache.org/log4net/release/faq.html#internalDebug –

我发现了这个问题。我一直在看错误的地方。当我检查instalation文件夹时,我注意到没有log4net.config! ......这就是为什么它在调试期间运行良好(因为配置在那里),但不是在部署之后!

对不起,并感谢您的所有帮助。

+0

犯了同样的错误。在Google搜索时找到了你的笔记,谢谢! :) – BjornW