我有一個控制檯應用程序,我正在轉換成一個Windows服務。作爲控制檯應用程序,我的log4net日誌記錄工作正常。但將其轉換爲Windows服務,我的log4net日誌已停止工作。log4net不能在Windows服務工作
我已經加入這個我在服務項目的AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
這是藉助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();
}
我有一個 「特殊」 的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>
任何想法或提示?
我解決了這個問題通過重寫appender。檢查此問題: http://stackoverflow.com/questions/1922430/how-do-you-make-log4net-output-to-current-working-directory/1922805#1922805 – RClemens 2013-05-10 23:50:29