log4net 在运行时启用调试日志记录
我试图在没有 app.config 的情况下进行调试。我有以下代码:
public static class Logging
{
private static ConsoleAppender GetConsoleAppender()
{
ConsoleAppender lAppender = new ConsoleAppender();
lAppender.Name = "Console";
lAppender.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
lAppender.Threshold = log4net.Core.Level.Error;
lAppender.ActivateOptions();
return lAppender;
}
private static FileAppender GetFileAppender()
{
FileAppender lAppender = new FileAppender();
lAppender.Name = "File";
lAppender.AppendToFile = true;
lAppender.File = "C:\\Enum.log";
lAppender.Layout = new
log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
lAppender.Threshold = log4net.Core.Level.All;
lAppender.ActivateOptions();
return lAppender;
}
static Logging()
{
Logger root;
root = ((Hierarchy)LogManager.GetRepository()).Root;
root.AddAppender(GetConsoleAppender());
root.AddAppender(GetFileAppender());
root.Level = log4net.Core.Level.All;
root.Repository.Configured = true;
}
}
然后我像这样进行调用:
private static ILog lLog = LogManager.GetLogger(typeof(Logging));
这似乎有效,但是 lLog 的状态将所有 isDebugEnabled、isErrorEnabled 等设置为 false。所以我正在寻找一种在运行时修改这些的方法。
注意:我没有使用 app.config,因为这是从 MMC 加载的 dll。
Im trying to get debugging working without an app.config. I have the following code:
public static class Logging
{
private static ConsoleAppender GetConsoleAppender()
{
ConsoleAppender lAppender = new ConsoleAppender();
lAppender.Name = "Console";
lAppender.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
lAppender.Threshold = log4net.Core.Level.Error;
lAppender.ActivateOptions();
return lAppender;
}
private static FileAppender GetFileAppender()
{
FileAppender lAppender = new FileAppender();
lAppender.Name = "File";
lAppender.AppendToFile = true;
lAppender.File = "C:\\Enum.log";
lAppender.Layout = new
log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
lAppender.Threshold = log4net.Core.Level.All;
lAppender.ActivateOptions();
return lAppender;
}
static Logging()
{
Logger root;
root = ((Hierarchy)LogManager.GetRepository()).Root;
root.AddAppender(GetConsoleAppender());
root.AddAppender(GetFileAppender());
root.Level = log4net.Core.Level.All;
root.Repository.Configured = true;
}
}
Then later I make a call like so:
private static ILog lLog = LogManager.GetLogger(typeof(Logging));
Which seems to work, however the state of lLog has all of the isDebugEnabled, isErrorEnabled, etc set to false. So im looking for a way to modify these at runtime.
Note: I am not using an app.config becuase this is a dll being loaded from MMC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不调用静态方法之一或访问静态类的静态属性,则静态初始化程序将不会被执行。在您以某种方式访问类的静态成员之前,您的记录器将不会被初始化。
If you don't call one of the static methods or access a static property of your static class, your static initializer is not going to be executed. Until you make an access to the static members of the class in some way, your logger will not be initialized.