我在 app.config 文件中使用了 log4net 代码,但该服务无法启动。如果我将代码用作单独的类并在它所做的服务的构造函数中调用它。如果我以后一种方式执行此操作,则不会记录日志。
我的配置文件代码是:-
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:\log\impersonationlog_JamochaService.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level [%thread] %type.%method - %message%n" />
</layout>
</appender>
<root>
<level value="All" />
<!-- If the following line is not included the log file
will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
</configuration>
如果我使用这个类而不是 app.config 代码,服务会启动,但日志记录并不总是发生。
public class Logger
{
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender roller = new RollingFileAppender();
roller.AppendToFile = false;
roller.File = "C:\\Logs\\EventLog.txt";
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
请帮助我克服这个问题并再次正确记录数据。
相关分类