一、導入nuget包
二、配置
簡單配置後就能夠寫本身的Job了json
注意:Hangfire.RecurringJobExtensions這個擴展支持兩種job添加方法:json配置文件和特性。但因爲時區使用的是string配置,致使Linux上時區不對,因此擴展一下job的加載方法:ide
若是不但願在一個job還沒執行完成的時候再次進入該job,添加以下過濾器: /// <summary> /// 每一個job都不可重入過濾器 /// </summary> public class NotReentryServerHangfireFilter : IServerFilter { /// <summary> /// 判斷job是否正在運行 /// </summary> static ConcurrentDictionary<string, DateTime> JobRunnings = new ConcurrentDictionary<string, DateTime>(); ILogger _logger; public NotReentryServerHangfireFilter(ILogger<NotReentryServerHangfireFilter> logger) { _logger = logger; } public void OnPerforming(PerformingContext filterContext) { var jobId = BuildJobId(filterContext.BackgroundJob); if(!JobRunnings.TryAdd(jobId, DateTime.Now)) { filterContext.Canceled = true; return; } _logger.LogInformation($"{jobId} starting..."); } public void OnPerformed(PerformedContext filterContext) { var jobId = BuildJobId(filterContext.BackgroundJob); JobRunnings.TryRemove(jobId, out var tmp); _logger.LogInformation($"{jobId} finished."); } public string BuildJobId(BackgroundJob job) { return $"{job.Job.Type.FullName}.{job.Job.Method.Name}"; } }
dashboard的受權取決於你的本身的身份認證機制了:
異常:
日誌:
日誌: public class HangfireLoggerProvider : ILogProvider { ILoggerFactory _loggerFactory; public HangfireLoggerProvider(ILoggerFactory loggerFactory) { _loggerFactory = loggerFactory; } public ILog GetLogger(string name) { return new HangfireLogger(_loggerFactory.CreateLogger(name)); } } public class HangfireLogger : ILog { ILogger _logger; public HangfireLogger(ILogger logger) { _logger = logger; } public bool Log(Hangfire.Logging.LogLevel logLevel, Func<string> messageFunc, Exception exception = null) { var msg = messageFunc?.Invoke(); if (string.IsNullOrWhiteSpace(msg) && exception == null) return false; switch (logLevel) { case Hangfire.Logging.LogLevel.Trace: _logger.LogTrace(0, exception, msg); break; case Hangfire.Logging.LogLevel.Debug: _logger.LogDebug(0, exception, msg); break; case Hangfire.Logging.LogLevel.Info: _logger.LogInformation(0, exception, msg); break; case Hangfire.Logging.LogLevel.Warn: _logger.LogWarning(0, exception, msg); break; case Hangfire.Logging.LogLevel.Error: _logger.LogError(0, exception, msg); break; case Hangfire.Logging.LogLevel.Fatal: _logger.LogCritical(0, exception, msg); break; default: break; } return true; } }