MemoryAppender.GetEvents:事件为空
MemoryAppender.GetEvents : Events Null
谁能解释为什么我没有从 memoryAppender 获得任何事件?
也就是说,events变量为Null。
public void Log(string message, Category category, Priority priority)
{
MemoryAppender memoryAppender = new MemoryAppender();
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(@"C:\Users\Username\Documents\GitHub\MassSpecStudio\MassSpecStudio 2.0\source\MassSpecStudio\Core\app.config"));
bool log4netIsConfigured = log4net.LogManager.GetRepository().Configured;
switch(category)
{
case Category.Debug:
log.Debug(message);
break;
case Category.Warn:
log.Warn(message);
break;
case Category.Exception:
log.Error(message);
break;
case Category.Info:
log.Info(message);
break;
}
var events = memoryAppender.GetEvents(); // events is Null.
int esize = events.Length;
foreach (LoggingEvent loggingEvent in events)
{
LogItem logItem = new LogItem(loggingEvent.TimeStamp, loggingEvent.Level, loggingEvent.RenderedMessage);
LogItems.Add(logItem);
}
}
*更新:
我在配置文件中提供了以下内容。我的 MemoryAppender 怎么没有正确连接到记录器?
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\Logger_.txt" />
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Once"/>
<param name="RollingStyle" value="Date"/>
<datePattern value="yyyy-MM-dd_HHmmss" />
<lockingModle type="log4net.Appender.FileAppender+MinimalLock"/>
<preserveLogFileNameExtension value="true"/>
<maxSizeRollBackups value="30" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="MemoryAppender" />
</root>
Log4Net 适用于 'Loggers' 和 'Appenders'。您需要将 Appender 连接到 Logger,以便它接收正在记录的消息。这可以在代码或配置文件中完成。通过使用配置文件初始化 log4net,然后在代码中创建 MemoryAppender
,您似乎同时做了两件事。
问题:
- 每次调用此函数时,您都在初始化 log4net。每个进程只需执行一次。
- 您在每次调用该函数时都创建了一个
MemoryAppender
的新实例,而您可能只需要一个。
- 主要问题: MemoryAppender 没有连接到记录器。如果您确实想在代码中执行此操作,请参见此处:Programmatically adding and removing log appenders in log4net
已更新以回答您修改后的问题:
要从 .config 文件中的单个 MemoryAppender
获取事件:
var appender = log4net.LogManager.GetRepository().GetAppenders().OfType<log4net.Appender.MemoryAppender>().Single(); // Assumes exactly one MemoryAppender
var events = appender.GetEvents();
谁能解释为什么我没有从 memoryAppender 获得任何事件? 也就是说,events变量为Null。
public void Log(string message, Category category, Priority priority)
{
MemoryAppender memoryAppender = new MemoryAppender();
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(@"C:\Users\Username\Documents\GitHub\MassSpecStudio\MassSpecStudio 2.0\source\MassSpecStudio\Core\app.config"));
bool log4netIsConfigured = log4net.LogManager.GetRepository().Configured;
switch(category)
{
case Category.Debug:
log.Debug(message);
break;
case Category.Warn:
log.Warn(message);
break;
case Category.Exception:
log.Error(message);
break;
case Category.Info:
log.Info(message);
break;
}
var events = memoryAppender.GetEvents(); // events is Null.
int esize = events.Length;
foreach (LoggingEvent loggingEvent in events)
{
LogItem logItem = new LogItem(loggingEvent.TimeStamp, loggingEvent.Level, loggingEvent.RenderedMessage);
LogItems.Add(logItem);
}
}
*更新:
我在配置文件中提供了以下内容。我的 MemoryAppender 怎么没有正确连接到记录器?
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\Logger_.txt" />
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Once"/>
<param name="RollingStyle" value="Date"/>
<datePattern value="yyyy-MM-dd_HHmmss" />
<lockingModle type="log4net.Appender.FileAppender+MinimalLock"/>
<preserveLogFileNameExtension value="true"/>
<maxSizeRollBackups value="30" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="MemoryAppender" />
</root>
Log4Net 适用于 'Loggers' 和 'Appenders'。您需要将 Appender 连接到 Logger,以便它接收正在记录的消息。这可以在代码或配置文件中完成。通过使用配置文件初始化 log4net,然后在代码中创建 MemoryAppender
,您似乎同时做了两件事。
问题:
- 每次调用此函数时,您都在初始化 log4net。每个进程只需执行一次。
- 您在每次调用该函数时都创建了一个
MemoryAppender
的新实例,而您可能只需要一个。 - 主要问题: MemoryAppender 没有连接到记录器。如果您确实想在代码中执行此操作,请参见此处:Programmatically adding and removing log appenders in log4net
已更新以回答您修改后的问题:
要从 .config 文件中的单个 MemoryAppender
获取事件:
var appender = log4net.LogManager.GetRepository().GetAppenders().OfType<log4net.Appender.MemoryAppender>().Single(); // Assumes exactly one MemoryAppender
var events = appender.GetEvents();