Last active
August 15, 2023 19:45
-
-
Save stefanolsen/724f05d1f04402e570e4abdf90154e89 to your computer and use it in GitHub Desktop.
Code listings for blog post about log correlation with Optimizely scheduled jobs and Application Insights. Read more at https://stefanolsen.com/posts/correlate-telemetry-for-scheduled-jobs-in-optimizely-cms/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using EPiServer.PlugIn; | |
using EPiServer.Scheduler; | |
using EPiServer.ServiceLocation; | |
using Microsoft.ApplicationInsights; | |
namespace DemoSite.ScheduledJobs; | |
[ScheduledPlugIn( | |
DisplayName = "Sample job", | |
GUID = "6C09139F-32B0-4501-ABFD-1B674DBE387A")] | |
[ServiceConfiguration(IncludeServiceAccessor = false)] | |
public class SampleScheduledJob : ScheduledJobBase, IDisposable | |
{ | |
private readonly TelemetryClient _telemetryClient; | |
private readonly ScheduledJobOperationScope _jobOperationScope; | |
public SampleScheduledJob(TelemetryClient telemetryClient) | |
{ | |
_jobOperationScope = new ScheduledJobOperationScope(telemetryClient, "Sample job"); | |
_telemetryClient = telemetryClient; | |
} | |
public override string Execute() | |
{ | |
// Do something that write log entries or makes dependency calls. | |
for (int i = 0; i < 200; i++) | |
{ | |
_telemetryClient.TrackTrace($"Iteration {i}"); | |
} | |
// Set Success to true or false, depending on the work that was done. | |
_jobOperationScope.Success = true; | |
return "DONE"; | |
} | |
public void Dispose() | |
{ | |
_jobOperationScope.Dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.ApplicationInsights; | |
using Microsoft.ApplicationInsights.DataContracts; | |
using Microsoft.ApplicationInsights.Extensibility; | |
namespace DemoSite.ScheduledJobs; | |
public sealed class ScheduledJobOperationScope : IDisposable | |
{ | |
private readonly IOperationHolder<DependencyTelemetry> _telemetry; | |
private readonly TelemetryClient _telemetryClient; | |
public ScheduledJobOperationScope(string jobName) | |
: this(new TelemetryClient(TelemetryConfiguration.CreateDefault()), jobName) | |
{ | |
} | |
public bool? Success | |
{ | |
get => _telemetry.Telemetry.Success; | |
set => _telemetry.Telemetry.Success = value; | |
} | |
public ScheduledJobOperationScope(TelemetryClient telemetryClient, string jobName) | |
{ | |
ArgumentNullException.ThrowIfNull(jobName); | |
_telemetryClient = telemetryClient ?? new TelemetryClient(TelemetryConfiguration.CreateDefault()); | |
if (!_telemetryClient.IsEnabled()) | |
{ | |
return; | |
} | |
_telemetry = _telemetryClient.StartOperation<DependencyTelemetry>($"JOB {jobName}"); | |
_telemetry.Telemetry.Type = "Background"; | |
} | |
public void Dispose() | |
{ | |
if (!_telemetryClient.IsEnabled()) | |
{ | |
return; | |
} | |
_telemetryClient.StopOperation(_telemetry); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment