Skip to content

Instantly share code, notes, and snippets.

@timgabrhel
Created February 24, 2016 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timgabrhel/f9cd4e66299ca108ad05 to your computer and use it in GitHub Desktop.
Save timgabrhel/f9cd4e66299ca108ad05 to your computer and use it in GitHub Desktop.
Application Insights basic logger
public class Logger
{
private static TelemetryClient telemetry = null;
public Logger(string instrumentationKey)
{
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
telemetry = new TelemetryClient();
telemetry.Context.InstrumentationKey = instrumentationKey;
}
public void LogTrace(string requestId, string operationName, string message)
{
telemetry.Context.Operation.Id = requestId;
telemetry.Context.Operation.Name = operationName;
telemetry.TrackTrace(requestId + " | " + operationName + " | " + message);
if (Debugger.IsAttached)
{
Debug.WriteLine(requestId + " | " + operationName + " | " + message);
}
}
public void LogException(string requestId, string operationName, Exception ex)
{
if (ex == null) return;
telemetry.Context.Operation.Id = requestId;
telemetry.Context.Operation.Name = operationName;
telemetry.TrackException(ex);
if (Debugger.IsAttached)
{
Debug.WriteLine(requestId + " | " + operationName + " | " + ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment