private void AddInstanceAppenders(string fileName) { //Get the logger repository hierarchy. var repository = LogManager.GetRepository(Assembly.GetCallingAssembly()) as Hierarchy; if (repository == null) { throw new Exception("log4net repository was not configured"); } #region Configure signalRAppender var instanceSignalRAppender = AddSignalRAppender(JobExecutionId.ToString()); #endregion #region Configure file appender FileAppender fileAppender = null; if (!string.IsNullOrWhiteSpace(fileName)) { fileAppender = new FileAppender { Name = Name + "FileAppender_" + JobExecutionId, File = fileName, AppendToFile = false, Threshold = Level.All, LockingModel = new FileAppender.MinimalLock(), ImmediateFlush = true }; var jsonLayout = new JsonLayout(); jsonLayout.ActivateOptions(); fileAppender.Layout = jsonLayout; fileAppender.ActivateOptions(); _logger.AddAppender(fileAppender); } #endregion if (repository.GetLogger(Name) is Logger log) { log.AddAppender(instanceSignalRAppender); if (fileAppender != null) { log.AddAppender(fileAppender); } } // Mark repository as configured and notify that is has changed. repository.Configured = true; repository.RaiseConfigurationChanged(EventArgs.Empty); }
private void RemoveInstanceAppenders() { var appenders = _logger.Appenders.ToArray() .Where(a => a.Name.EndsWith(JobExecutionId.ToString(), StringComparison.InvariantCultureIgnoreCase)); foreach (var appender in appenders) { appender.Close(); _logger.RemoveAppender(appender); } }
private void SaveJobExecutionIdForLogging() { ThreadContext.Properties[JobExecutionIdKey] = JobExecutionId != Guid.Empty ? JobExecutionId.ToString() : null; }