lmk*_*mkk 7 c# logging windows-services
我有一个控制台应用程序,我正在转换为Windows服务.作为控制台应用程序,我的log4net日志记录工作正常.但是将它转换为Windows服务,我的log4net日志记录已停止工作.
我已将此添加到服务项目中的assemblyInfo.cs中:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Run Code Online (Sandbox Code Playgroud)
这是我的onstart和onstop服务类:
private static log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private Builder _builder;
public YCSWebServerService()
{
InitializeComponent();
_builder = new Builder();
}
protected override void OnStart(string[] args)
{
_log.Info("YCSWebServerService started");
_builder.Start();
}
protected override void OnStop()
{
_log.Info("YCSWebServerService stopped");
_builder.Stop();
}
Run Code Online (Sandbox Code Playgroud)
我有一个"特定的"log4net配置文件添加到我的服务项目:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="EventLogAppender" />
</root>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<threshold value="DEBUG" />
<applicationName value="Lantic YCS WebServer" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="(%-5level %date{hh}:%date{mm}:%date{ss} [%thread] %logger [%property{NDC}] ) %message %n" />
</layout>
</appender>
</log4net>
</configuration>
Run Code Online (Sandbox Code Playgroud)
任何想法或提示?
您刚刚在app.config文件中添加了log4net部分吗?在您的问题中,您提到您有"特定的log4net配置文件",但您提供的示例看起来像app.config的全部内容.如果是app.config,那么你可以在AssemblyInfo.cs中使用更简单的字符串:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Run Code Online (Sandbox Code Playgroud)
另外在我app.config中的部分添加requirePermission ="false"帮助我解决了log4net没有记录到Windows服务文件的类似问题(请参阅额外属性 - requirePermission ="false"):
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
</configSections>
Run Code Online (Sandbox Code Playgroud)