I just want to extend baywet's answer.
If you can, I suggest to use the existing nuget package to extend Application Insights with OWIN. In that case you need at least .NET Framework 4.6.1.
Otherwise you can use "my" solution. I have just mixed theese two answers:
What I needed was logging exceptions and requests. This is what I have done.. (Here I write the essential things to let you to write a working solution).
I have created two different objects trackers: TrackerException
and a TrackerRequest
. Both implement the Proxy Pattern. In this way I do not have to apparently care what I have decided to track. I just let you see a simpler implementation of one tracker:
public class TrackerException : ITracker
{
private readonly TelemetryClient _telemetryClient;
public TrackerException(TelemetryClient telemetryClient)
{
this._telemetryClient = telemetryClient;
}
public void Track(ITelemetryObject telemetry)
{
if (telemetry is TelemetryExceptionObject)
{
Exception ex = ((TelemetryExceptionObject)telemetry).Exception;
this._telemetryClient.TrackException(ex);
}
}
}
I use the trackers inside my custom Owin middleware:
public class ApplicationInsightsMiddleware : OwinMiddleware
{
private TrackingOptions _trackingOptions;
public ApplicationInsightsMiddleware(OwinMiddleware next)
: base(next)
{ }
public ApplicationInsightsMiddleware(OwinMiddleware next, TrackingOptions trackingOptions)
: base(next)
{
this._trackingOptions = trackingOptions;
}
public override async Task Invoke(IOwinContext context)
{
var client = new TelemetryClient();
this._trackingOptions.TelemetryClient = client;
// Start Time Tracking
var sw = new Stopwatch();
var startTime = DateTimeOffset.Now;
sw.Start();
this._trackingOptions.TrackRequest.ReadRequestBody(context.Request);
try
{
await Next.Invoke(context);
}
catch (Exception ex)
{
ITelemetryObject exception = new TelemetryExceptionObject(ex);
this._trackingOptions.TrackException.Track(exception);
throw ex;
}
RequestTelemetry requestTelemetry = this._trackingOptions.TrackRequest.CreateTelemetryRequest(context.Request, context.Response, startTime, sw.Elapsed);
ITelemetryObject request = new TelemetryRequestObject(requestTelemetry);
this._trackingOptions.TrackRequest.Track(request);
sw.Stop();
}
}
TrackingOptions
is a class that just enable or not a tracker... Here its implementation:
public class TrackingOptions
{
//Here I initialize all my trackers
private ProxyTrackerException _trackException;
internal ProxyTrackerException TrackException { get { return _trackException; } }
private TelemetryClient _telemetryClient;
internal TelemetryClient TelemetryClient
{
get { return _telemetryClient; }
set { _telemetryClient = value; }
}
public TrackingOptions(bool enableTrackingRequest, bool enableTrackingException)
{
this.InitializeExceptionTracker(enableTrackingException);
//...
}
private void InitializeExceptionTracker(bool enableTrackingException)
{
this._trackException = new ProxyTrackerException(enableTrackingException, this._telemetryClient);
}
}
In the end I have written an extension method for Owin
public static void UseApplicationInsights(this IAppBuilder app, TrackingOptions options)
{
app.Use(typeof(ApplicationInsightsMiddleware), options);
}
In the startup of my project I add my new middleware:
app.UseApplicationInsights(
new TrackingOptions(enableTrackingRequest: true, enableTrackingException: true)
);
However, this is a temporary solution for me... As soon as possible I will update my framework and install the existing nuget package.