2017-07-07 121 views
1

爲了製作更復雜的日誌,我決定將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 –

回答

1

我發現了這個問題。我一直在看錯誤的地方。當我檢查instalation文件夾時,我注意到沒有log4net.config! ......這就是爲什麼它在調試期間運行良好(因爲配置在那裏),但不是在部署之後!

對不起,並感謝您的所有幫助。

+0

犯了同樣的錯誤。在Google搜索時找到了你的筆記,謝謝! :) – BjornW

相關問題