在 Quartz.NET 调度程序中配置日志记录,可以通过以下几个步骤来完成:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
using log4net;
public class Global : HttpApplication
{
private static readonly ILog log = LogManager.GetLogger(typeof(Global));
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
}
using log4net;
using Quartz;
public class Log4NetLogProvider : ILog
{
private readonly ILog log;
public Log4NetLogProvider(ILog log)
{
this.log = log;
}
public bool IsDebugEnabled => log.IsDebugEnabled;
public bool IsInfoEnabled => log.IsInfoEnabled;
public bool IsWarnEnabled => log.IsWarnEnabled;
public bool IsErrorEnabled => log.IsErrorEnabled;
public bool IsFatalEnabled => log.IsFatalEnabled;
public void Debug(object message)
{
log.Debug(message);
}
public void Debug(object message, Exception exception)
{
log.Debug(message, exception);
}
public void DebugFormat(string format, params object[] args)
{
log.DebugFormat(format, args);
}
public void DebugFormat(string format, object arg0)
{
log.DebugFormat(format, arg0);
}
public void DebugFormat(string format, object arg0, object arg1)
{
log.DebugFormat(format, arg0, arg1);
}
public void DebugFormat(string format, object arg0, object arg1, object arg2)
{
log.DebugFormat(format, arg0, arg1, arg2);
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
log.DebugFormat(provider, format, args);
}
public void Info(object message)
{
log.Info(message);
}
public void Info(object message, Exception exception)
{
log.Info(message, exception);
}
public void InfoFormat(string format, params object[] args)
{
log.InfoFormat(format, args);
}
public void InfoFormat(string format, object arg0)
{
log.InfoFormat(format, arg0);
}
public void InfoFormat(string format, object arg0, object arg1)
{
log.InfoFormat(format, arg0, arg1);
}
public void InfoFormat(string format, object arg0, object arg1, object arg2)
{
log.InfoFormat(format, arg0, arg1, arg2);
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
log.InfoFormat(provider, format, args);
}
public void Warn(object message)
{
log.Warn(message);
}
public void Warn(object message, Exception exception)
{
log.Warn(message, exception);
}
public void WarnFormat(string format, params object[] args)
{
log.WarnFormat(format, args);
}
public void WarnFormat(string format, object arg0)
{
log.WarnFormat(format, arg0);
}
public void WarnFormat(string format, object arg0, object arg1)
{
log.WarnFormat(format, arg0, arg1);
}
public void WarnFormat(string format, object arg0, object arg1, object arg2)
{
log.WarnFormat(format, arg0, arg1, arg2);
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
log.WarnFormat(provider, format, args);
}
public void Error(object message)
{
log.Error(message);
}
public void Error(object message, Exception exception)
{
log.Error(message, exception);
}
public void ErrorFormat(string format, params object[] args)
{
log.ErrorFormat(format, args);
}
public void ErrorFormat(string format, object arg0)
{
log.ErrorFormat(format, arg0);
}
public void ErrorFormat(string format, object arg0, object arg1)
{
log.ErrorFormat(format, arg0, arg1);
}
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
log.ErrorFormat(format, arg0, arg1, arg2);
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
log.ErrorFormat(provider, format, args);
}
public void Fatal(object message)
{
log.Fatal(message);
}
public void Fatal(object message, Exception exception)
{
log.Fatal(message, exception);
}
public void FatalFormat(string format, params object[] args)
{
log.FatalFormat(format, args);
}
public void FatalFormat(string format, object arg0)
{
log.FatalFormat(format, arg0);
}
public void FatalFormat(string format, object arg0, object arg1)
{
log.FatalFormat(format, arg0, arg1);
}
public void FatalFormat(string format, object arg0, object arg1, object arg2)
{
log.FatalFormat(format, arg0, arg1, arg2);
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
log.FatalFormat(provider, format, args);
}
}
using Quartz;
using Quartz.Impl;
using Quartz.Logging;
public class Global : HttpApplication
{
private static readonly ILog log = LogManager.GetLogger(typeof(Global));
private static readonly IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
LogProvider.SetCurrentLogProvider(new Log4NetLogProvider(LogManager.GetLogger(typeof(Log4NetLogProvider))));
scheduler.Start();
}
}
这样,就可以在 Quartz.NET 调度程序中配置日志记录了。
领取专属 10元无门槛券
手把手带您无忧上云