Log4Net在VisualStudio调试/发布环境中能正常工作,但是在部署后无法工作。

3
为了记录更复杂的日志,我决定在我的WPF桌面应用程序中使用Log4Net。它完美地工作,既创建“Debug”消息到“Output”,又将所有日志写入文件中...
但是,如果我部署或安装它(使用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")]

感谢您的时间,如果需要我添加更多细节,请在评论中指出。
顺便说一句:软件安装在AppData内部,因此不应出现任何文件权限问题,因为我正在使用另一个框架来自动更新应用程序。
再次提醒:我使用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!)


目标机器上无法创建DesktopApp.log的原因是什么? - Ross Bush
该软件安装在Appdata的本地文件夹中,因此不应出现任何权限/文件写入问题。我之前使用过“手动”日志,它运行得非常顺畅。 - ironmagician
你可以使用log4net来调试内部发生的情况。这可能会给你一些见解。请访问以下链接 - https://logging.apache.org/log4net/release/faq.html#internalDebug - Varun Mehta
谢谢。我会去查看的。如果我发现了什么新的东西,我会更新的。 - ironmagician
1个回答

1
我找到了问题所在。我一直看错了地方。当我检查安装文件夹时,我注意到没有log4net.config!这就是为什么在调试期间它运行良好(因为配置存在),但部署后没有工作的原因!
很抱歉打扰你们了,感谢你们所有的帮助。

犯了同样的错误。在谷歌搜索时找到了你的笔记,谢谢老兄! :) - BjornW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接