private static ILoggerFactory ConfigureLogging(JobBase job) { var loggerFactory = LoggingSetup.CreateLoggerFactory(LoggingSetup.CreateDefaultLoggerConfiguration(true)); var logger = loggerFactory.CreateLogger(job.GetType()); job.SetLogger(loggerFactory, logger); _logger = loggerFactory.CreateLogger(typeof(JobRunner)); return(loggerFactory); }
private static ApplicationInsightsConfiguration ConfigureApplicationInsights(JobBase job, IDictionary <string, string> jobArgsDictionary) { ApplicationInsightsConfiguration applicationInsightsConfiguration; var instrumentationKey = JobConfigurationManager.TryGetArgument( jobArgsDictionary, JobArgumentNames.InstrumentationKey); var heartbeatIntervalSeconds = JobConfigurationManager.TryGetIntArgument( jobArgsDictionary, JobArgumentNames.HeartbeatIntervalSeconds); if (heartbeatIntervalSeconds.HasValue) { applicationInsightsConfiguration = ApplicationInsights.Initialize( instrumentationKey, TimeSpan.FromSeconds(heartbeatIntervalSeconds.Value)); } else { applicationInsightsConfiguration = ApplicationInsights.Initialize(instrumentationKey); } // Determine job and instance name, for logging. var jobName = job.GetType().Assembly.GetName().Name; var instanceName = JobConfigurationManager.TryGetArgument(jobArgsDictionary, JobArgumentNames.InstanceName) ?? jobName; applicationInsightsConfiguration.TelemetryConfiguration.TelemetryInitializers.Add( new JobPropertiesTelemetryInitializer(jobName, instanceName, job.GlobalTelemetryDimensions)); applicationInsightsConfiguration.TelemetryConfiguration .ApplicationIdProvider = new ApplicationInsightsApplicationIdProvider(); job.SetApplicationInsightsConfiguration(applicationInsightsConfiguration); return(applicationInsightsConfiguration); }
private static async Task Run(JobBase job, string[] commandLineArgs, bool?runContinuously) { if (commandLineArgs.Length > 0 && string.Equals(commandLineArgs[0], "-" + JobArgumentNames.Dbg, StringComparison.OrdinalIgnoreCase)) { commandLineArgs = commandLineArgs.Skip(1).ToArray(); Debugger.Launch(); } // Configure logging before Application Insights is enabled. // This is done so, in case Application Insights fails to initialize, we still see output. var loggerFactory = ConfigureLogging(job); try { _logger.LogInformation("Started..."); // Get the args passed in or provided as an env variable based on jobName as a dictionary of <string argName, string argValue> var jobArgsDictionary = JobConfigurationManager.GetJobArgsDictionary( ServiceContainer, loggerFactory.CreateLogger(typeof(JobConfigurationManager)), commandLineArgs); // Determine job and instance name, for logging. var jobName = job.GetType().Assembly.GetName().Name; var instanceName = JobConfigurationManager.TryGetArgument(jobArgsDictionary, JobArgumentNames.InstanceName) ?? jobName; TelemetryConfiguration.Active.TelemetryInitializers.Add(new JobNameTelemetryInitializer(jobName, instanceName)); // Setup logging if (!ApplicationInsights.Initialized) { string instrumentationKey = JobConfigurationManager.TryGetArgument(jobArgsDictionary, JobArgumentNames.InstrumentationKey); if (!string.IsNullOrWhiteSpace(instrumentationKey)) { ApplicationInsights.Initialize(instrumentationKey); } } // Configure our logging again with Application Insights initialized. loggerFactory = ConfigureLogging(job); var hasOnceArgument = JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, JobArgumentNames.Once); if (runContinuously.HasValue && hasOnceArgument) { _logger.LogWarning( $"This job is designed to {(runContinuously.Value ? "run continuously" : "run once")} so " + $"the -{JobArgumentNames.Once} argument is {(runContinuously.Value ? "ignored" : "redundant")}."); } runContinuously = runContinuously ?? !hasOnceArgument; var reinitializeAfterSeconds = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, JobArgumentNames.ReinitializeAfterSeconds); var sleepDuration = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, JobArgumentNames.Sleep); // sleep is in milliseconds if (!sleepDuration.HasValue) { sleepDuration = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, JobArgumentNames.Interval); if (sleepDuration.HasValue) { sleepDuration = sleepDuration.Value * 1000; // interval is in seconds } } if (!sleepDuration.HasValue) { if (runContinuously.Value) { _logger.LogInformation("SleepDuration is not provided or is not a valid integer. Unit is milliSeconds. Assuming default of 5000 ms..."); } sleepDuration = 5000; } else if (!runContinuously.Value) { _logger.LogWarning( $"The job is designed to run once so the -{JobArgumentNames.Sleep} and " + $"-{JobArgumentNames.Interval} arguments are ignored."); } if (!reinitializeAfterSeconds.HasValue) { _logger.LogInformation( $"{JobArgumentNames.ReinitializeAfterSeconds} command line argument is not provided or is not a valid integer. " + "The job will reinitialize on every iteration"); } else if (!runContinuously.Value) { _logger.LogWarning( $"The job is designed to run once so the -{JobArgumentNames.ReinitializeAfterSeconds} " + $"argument is ignored."); } // Ensure that SSLv3 is disabled and that Tls v1.2 is enabled. ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3; ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; // Run the job loop await JobLoop(job, runContinuously.Value, sleepDuration.Value, reinitializeAfterSeconds, jobArgsDictionary); } catch (Exception ex) { _logger.LogError("Job runner threw an exception: {Exception}", ex); } Trace.Close(); TelemetryConfiguration.Active.TelemetryChannel.Flush(); }