有没有办法关闭Hangfire与serilog的日志记录?我们正在使用自己的抽象,我不希望所有这些额外的噪音来自Hangfire记录器,同时使用serilog。
// INIT call under web project
namespace MYApp.Web.App_Start
{
public static class Bootstrapper
{
public static void Run()
{
Core.Logging.Serilog.SetConfiguration();
}
}
}
//设置config方法的项目
namespace MYApp.Core.Logging
{
public static class Serilog
{
public static void SetConfiguration()
{
Log.Logger = new LoggerConfiguration()
//.WriteTo.File(@"c:\log-.txt", rollingInterval: RollingInterval.Minute, shared: true)
.WriteTo.Email(new EmailConnectionInfo()
{
FromEmail = "[email protected]",
MailServer = "smtp.gmail.com",
ToEmail = "[email protected]",
NetworkCredentials = new NetworkCredential("[email protected]", "xxxxxxxxx"),
Port = 587,
EnableSsl = true,
EmailSubject = "YYYYYY: Error Log"
}, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {NewLine} {Message:lj}{NewLine}{Exception}")
.Filter.ByExcluding(Matching.FromSource("Hangfire"))
.CreateLogger();
}
}
}
定义不记录任何内容的记录器和日志提供程序:
using Hangfire;
using Hangfire.Logging;
public class NoLoggingProvider : ILogProvider {
public ILog GetLogger(string name) {
return new NoLoggingLogger();
}
}
public class NoLoggingLogger : ILog {
public bool Log(LogLevel logLevel, Func<string> messageFunc, Exception exception = null) {
return false;
}
}
并配置Hangfire在Startup
类中使用它:
using Hangfire;
public class Startup {
public void Configuration(IAppBuilder app) {
GlobalConfiguration.Configuration.UseLogProvider(new NoLoggingProvider());
// the rest of your configuration...
}
}
您可以使用Filter表达式排除整个Hangfire命名空间(假设它全部在一个命名空间下 - 我以前从未使用它):
.Filter.ByExcluding(Matching.FromSource("Hangfire"))