Log4Net 在 VisualStudio Debug/Release 上工作,但在部署后不工作
Log4Net works on VisualStudio Debug/Release, but not after deployment
为了制作更复杂的日志,我决定将 Log4Net 用于我的 WPF 桌面应用程序。它完美地工作,在 'Output' 处创建 'Debug' 消息并将所有日志写入文件...
但是,如果我部署 it/install 它(使用 visual studio 的向导创建 .MSI 设置),它就会停止工作。我在该文件夹或计算机上的任何地方都找不到任何相关文件。
我正在使用 Caliburn.Micro 来组织我的代码。为了获得日志,我 'centralized' 通过使用名为 "Logging.cs".
的 public 静态 class
现在 "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里面的,应该不会出现文件权限问题,因为我是用另外一个框架来自动更新app的。
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!)
我发现了问题。我一直在看错地方。当我检查安装文件夹时,我发现没有 log4net.config! ...这就是为什么它在调试期间运行良好(因为配置在那里)但在部署之后却不行!
抱歉打扰了,谢谢大家的帮助。
为了制作更复杂的日志,我决定将 Log4Net 用于我的 WPF 桌面应用程序。它完美地工作,在 'Output' 处创建 'Debug' 消息并将所有日志写入文件...
但是,如果我部署 it/install 它(使用 visual studio 的向导创建 .MSI 设置),它就会停止工作。我在该文件夹或计算机上的任何地方都找不到任何相关文件。
我正在使用 Caliburn.Micro 来组织我的代码。为了获得日志,我 'centralized' 通过使用名为 "Logging.cs".
的 public 静态 class现在 "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里面的,应该不会出现文件权限问题,因为我是用另外一个框架来自动更新app的。
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!)
我发现了问题。我一直在看错地方。当我检查安装文件夹时,我发现没有 log4net.config! ...这就是为什么它在调试期间运行良好(因为配置在那里)但在部署之后却不行!
抱歉打扰了,谢谢大家的帮助。