Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Commit c9b04f5

Browse files
author
Timothy Mothra
authored
Merge pull request #858 from Microsoft/develop
merge DEVELOP to MASTER (prep 2.7.0-beta3)
2 parents 1d90711 + cbbf53d commit c9b04f5

23 files changed

+484
-95
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 2.7.0-beta3
4+
- [Enables Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider by default. If ApplicationInsightsLoggerProvider was enabled previously using ILoggerFactory extension method, please remove it to prevent duplicate logs.](https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/854)
5+
- [Remove reference to Microsoft.Extensions.DiagnosticAdapter and use DiagnosticSource subscription APIs directly](https://github.com/Microsoft/ApplicationInsights-aspnetcore/pull/852)
6+
- [Fix: NullReferenceException in ApplicationInsightsLogger.Log when exception contains a Data entry with a null value](https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/848)
7+
38
## Version 2.7.0-beta2
49
- Added NetStandard2.0 target.
510
- Updated Web/Base SDK version dependency to 2.10.0-beta2

Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
- [Microsoft.ApplicationInsights.AspNetCore](https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore/)
44
[![Nuget](https://img.shields.io/nuget/vpre/Microsoft.ApplicationInsights.AspNetCore.svg)](https://nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore)
55

6+
Windows: [![Build Status](https://mseng.visualstudio.com/AppInsights/_apis/build/status/ChuckNorris/AI_ASPNETCore_Develop?branchName=develop)](https://mseng.visualstudio.com/AppInsights/_build/latest?definitionId=3717&branchName=develop)
7+
8+
Linux :[![Build Status](https://mseng.visualstudio.com/AppInsights/_apis/build/status/ChuckNorris/AI-AspNetCoreSDK-develop-linux?branchName=develop)](https://mseng.visualstudio.com/AppInsights/_build/latest?definitionId=6273&branchName=develop)
9+
610

711
Microsoft Application Insights for ASP.NET Core applications
812
=============================================================

src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/HostingDiagnosticListener.cs

Lines changed: 96 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
namespace Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.Globalization;
67
using System.Linq;
78
using System.Net.Http.Headers;
89
using System.Text;
910
using Extensibility.Implementation.Tracing;
11+
using Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners.Implementation;
1012
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
1113
using Microsoft.ApplicationInsights.Common;
1214
using Microsoft.ApplicationInsights.DataContracts;
1315
using Microsoft.ApplicationInsights.Extensibility;
1416
using Microsoft.ApplicationInsights.Extensibility.Implementation;
1517
using Microsoft.ApplicationInsights.Extensibility.W3C;
1618
using Microsoft.AspNetCore.Http;
17-
using Microsoft.Extensions.DiagnosticAdapter;
1819
using Microsoft.Extensions.Primitives;
1920

2021
/// <summary>
@@ -38,6 +39,24 @@ internal class HostingDiagnosticListener : IApplicationInsightDiagnosticListener
3839
private static readonly ActiveSubsciptionManager SubscriptionManager = new ActiveSubsciptionManager();
3940
private const string ActivityCreatedByHostingDiagnosticListener = "ActivityCreatedByHostingDiagnosticListener";
4041

42+
#region fetchers
43+
44+
// fetch is unique per event and per property
45+
private readonly PropertyFetcher httpContextFetcherStart = new PropertyFetcher("HttpContext");
46+
private readonly PropertyFetcher httpContextFetcherStop = new PropertyFetcher("HttpContext");
47+
private readonly PropertyFetcher httpContextFetcherBeginRequest = new PropertyFetcher("httpContext");
48+
private readonly PropertyFetcher httpContextFetcherEndRequest = new PropertyFetcher("httpContext");
49+
private readonly PropertyFetcher httpContextFetcherDiagExceptionUnhandled = new PropertyFetcher("httpContext");
50+
private readonly PropertyFetcher httpContextFetcherDiagExceptionHandled = new PropertyFetcher("httpContext");
51+
private readonly PropertyFetcher httpContextFetcherHostingExceptionUnhandled = new PropertyFetcher("httpContext");
52+
private readonly PropertyFetcher exceptionFetcherDiagExceptionUnhandled = new PropertyFetcher("exception");
53+
private readonly PropertyFetcher exceptionFetcherDiagExceptionHandled = new PropertyFetcher("exception");
54+
private readonly PropertyFetcher exceptionFetcherHostingExceptionUnhandled = new PropertyFetcher("exception");
55+
56+
private readonly PropertyFetcher timestampFetcherBeginRequest = new PropertyFetcher("timestamp");
57+
private readonly PropertyFetcher timestampFetcherEndRequest = new PropertyFetcher("timestamp");
58+
#endregion
59+
4160
/// <summary>
4261
/// Initializes a new instance of the <see cref="T:HostingDiagnosticListener"/> class.
4362
/// </summary>
@@ -72,19 +91,9 @@ public void OnSubscribe()
7291
/// <inheritdoc/>
7392
public string ListenerName { get; } = "Microsoft.AspNetCore";
7493

75-
/// <summary>
76-
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HttpRequestIn' event.
77-
/// </summary>
78-
[DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn")]
79-
public void OnHttpRequestIn()
80-
{
81-
// do nothing, just enable the diagnostic source
82-
}
83-
8494
/// <summary>
8595
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HttpRequestIn.Start' event.
8696
/// </summary>
87-
[DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start")]
8897
public void OnHttpRequestInStart(HttpContext httpContext)
8998
{
9099
if (this.client.IsEnabled())
@@ -176,7 +185,6 @@ public void OnHttpRequestInStart(HttpContext httpContext)
176185
/// <summary>
177186
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop' event.
178187
/// </summary>
179-
[DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop")]
180188
public void OnHttpRequestInStop(HttpContext httpContext)
181189
{
182190
EndRequest(httpContext, Stopwatch.GetTimestamp());
@@ -185,7 +193,6 @@ public void OnHttpRequestInStop(HttpContext httpContext)
185193
/// <summary>
186194
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.BeginRequest' event.
187195
/// </summary>
188-
[DiagnosticName("Microsoft.AspNetCore.Hosting.BeginRequest")]
189196
public void OnBeginRequest(HttpContext httpContext, long timestamp)
190197
{
191198
if (this.client.IsEnabled() && !this.enableNewDiagnosticEvents)
@@ -282,7 +289,6 @@ public void OnBeginRequest(HttpContext httpContext, long timestamp)
282289
/// <summary>
283290
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.EndRequest' event.
284291
/// </summary>
285-
[DiagnosticName("Microsoft.AspNetCore.Hosting.EndRequest")]
286292
public void OnEndRequest(HttpContext httpContext, long timestamp)
287293
{
288294
if (!this.enableNewDiagnosticEvents)
@@ -294,7 +300,6 @@ public void OnEndRequest(HttpContext httpContext, long timestamp)
294300
/// <summary>
295301
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.UnhandledException' event.
296302
/// </summary>
297-
[DiagnosticName("Microsoft.AspNetCore.Hosting.UnhandledException")]
298303
public void OnHostingException(HttpContext httpContext, Exception exception)
299304
{
300305
this.OnException(httpContext, exception);
@@ -310,7 +315,6 @@ public void OnHostingException(HttpContext httpContext, Exception exception)
310315
/// <summary>
311316
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HandledException' event.
312317
/// </summary>
313-
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.HandledException")]
314318
public void OnDiagnosticsHandledException(HttpContext httpContext, Exception exception)
315319
{
316320
this.OnException(httpContext, exception);
@@ -319,7 +323,6 @@ public void OnDiagnosticsHandledException(HttpContext httpContext, Exception exc
319323
/// <summary>
320324
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Diagnostics.UnhandledException' event.
321325
/// </summary>
322-
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.UnhandledException")]
323326
public void OnDiagnosticsUnhandledException(HttpContext httpContext, Exception exception)
324327
{
325328
this.OnException(httpContext, exception);
@@ -559,5 +562,81 @@ public void Dispose()
559562
{
560563
SubscriptionManager.Detach(this);
561564
}
565+
566+
public void OnNext(KeyValuePair<string, object> value)
567+
{
568+
HttpContext httpContext = null;
569+
Exception exception = null;
570+
long? timestamp = null;
571+
572+
switch (value.Key)
573+
{
574+
case "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start":
575+
httpContext = this.httpContextFetcherStart.Fetch(value.Value) as HttpContext;
576+
if (httpContext != null)
577+
{
578+
this.OnHttpRequestInStart(httpContext);
579+
}
580+
break;
581+
case "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop":
582+
httpContext = this.httpContextFetcherStop.Fetch(value.Value) as HttpContext;
583+
if (httpContext != null)
584+
{
585+
this.OnHttpRequestInStop(httpContext);
586+
}
587+
break;
588+
case "Microsoft.AspNetCore.Hosting.BeginRequest":
589+
httpContext = this.httpContextFetcherBeginRequest.Fetch(value.Value) as HttpContext;
590+
timestamp = this.timestampFetcherBeginRequest.Fetch(value.Value) as long?;
591+
if (httpContext != null && timestamp.HasValue)
592+
{
593+
this.OnBeginRequest(httpContext, timestamp.Value);
594+
}
595+
break;
596+
case "Microsoft.AspNetCore.Hosting.EndRequest":
597+
httpContext = this.httpContextFetcherEndRequest.Fetch(value.Value) as HttpContext;
598+
timestamp = this.timestampFetcherEndRequest.Fetch(value.Value) as long?;
599+
if (httpContext != null && timestamp.HasValue)
600+
{
601+
this.OnEndRequest(httpContext, timestamp.Value);
602+
}
603+
break;
604+
case "Microsoft.AspNetCore.Diagnostics.UnhandledException":
605+
httpContext = this.httpContextFetcherDiagExceptionUnhandled.Fetch(value.Value) as HttpContext;
606+
exception = this.exceptionFetcherDiagExceptionUnhandled.Fetch(value.Value) as Exception;
607+
if (httpContext != null && exception != null)
608+
{
609+
this.OnDiagnosticsUnhandledException(httpContext, exception);
610+
}
611+
break;
612+
case "Microsoft.AspNetCore.Diagnostics.HandledException":
613+
httpContext = this.httpContextFetcherDiagExceptionHandled.Fetch(value.Value) as HttpContext;
614+
exception = this.exceptionFetcherDiagExceptionHandled.Fetch(value.Value) as Exception;
615+
if (httpContext != null && exception != null)
616+
{
617+
this.OnDiagnosticsHandledException(httpContext, exception);
618+
}
619+
break;
620+
case "Microsoft.AspNetCore.Hosting.UnhandledException":
621+
httpContext = this.httpContextFetcherHostingExceptionUnhandled.Fetch(value.Value) as HttpContext;
622+
exception = this.exceptionFetcherHostingExceptionUnhandled.Fetch(value.Value) as Exception;
623+
if (httpContext != null && exception != null)
624+
{
625+
this.OnHostingException(httpContext, exception);
626+
}
627+
break;
628+
}
629+
}
630+
631+
/// <inheritdoc />
632+
public void OnError(Exception error)
633+
{
634+
}
635+
636+
/// <inheritdoc />
637+
public void OnCompleted()
638+
{
639+
}
640+
562641
}
563642
}

src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/IApplicationInsightDiagnosticListener.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
namespace Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners
22
{
33
using System;
4+
using System.Collections.Generic;
45

56
/// <summary>
67
/// Base diagnostic listener type for Application Insight
78
/// </summary>
8-
internal interface IApplicationInsightDiagnosticListener : IDisposable
9+
internal interface IApplicationInsightDiagnosticListener : IDisposable, IObserver<KeyValuePair<string, object>>
910
{
1011
/// <summary>
1112
/// Gets a value indicating which listener this instance should be subscribed to

src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/MvcDiagnosticsListener.cs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ namespace Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners.Implementation;
67
using Microsoft.ApplicationInsights.DataContracts;
78
using Microsoft.AspNetCore.Http;
8-
using Microsoft.Extensions.DiagnosticAdapter;
99

1010
/// <summary>
1111
/// <see cref="IApplicationInsightDiagnosticListener"/> implementation that listens for evens specific to AspNetCore Mvc layer
@@ -15,17 +15,20 @@ public class MvcDiagnosticsListener : IApplicationInsightDiagnosticListener
1515
/// <inheritdoc />
1616
public string ListenerName { get; } = "Microsoft.AspNetCore";
1717

18+
private readonly PropertyFetcher httpContextFetcher = new PropertyFetcher("httpContext");
19+
private readonly PropertyFetcher routeDataFetcher = new PropertyFetcher("routeData");
20+
private readonly PropertyFetcher routeValuesFetcher = new PropertyFetcher("Values");
21+
1822
/// <summary>
1923
/// Diagnostic event handler method for 'Microsoft.AspNetCore.Mvc.BeforeAction' event
2024
/// </summary>
21-
[DiagnosticName("Microsoft.AspNetCore.Mvc.BeforeAction")]
22-
public void OnBeforeAction(HttpContext httpContext, IRouteData routeData)
25+
public void OnBeforeAction(HttpContext httpContext, IDictionary<string, object> routeValues)
2326
{
2427
var telemetry = httpContext.Features.Get<RequestTelemetry>();
2528

2629
if (telemetry != null && string.IsNullOrEmpty(telemetry.Name))
2730
{
28-
string name = this.GetNameFromRouteContext(routeData);
31+
string name = this.GetNameFromRouteContext(routeValues);
2932

3033
if (!string.IsNullOrEmpty(name))
3134
{
@@ -35,19 +38,12 @@ public void OnBeforeAction(HttpContext httpContext, IRouteData routeData)
3538
}
3639
}
3740

38-
/// <inheritdoc />
39-
public void OnSubscribe()
40-
{
41-
}
42-
43-
private string GetNameFromRouteContext(IRouteData routeData)
41+
private string GetNameFromRouteContext(IDictionary<string, object> routeValues)
4442
{
4543
string name = null;
4644

47-
if (routeData.Values.Count > 0)
45+
if (routeValues.Count > 0)
4846
{
49-
var routeValues = routeData.Values;
50-
5147
object controller;
5248
routeValues.TryGetValue("controller", out controller);
5349
string controllerString = (controller == null) ? string.Empty : controller.ToString();
@@ -98,19 +94,40 @@ private string GetNameFromRouteContext(IRouteData routeData)
9894
return name;
9995
}
10096

101-
public void Dispose()
97+
/// <inheritdoc />
98+
public void OnSubscribe()
10299
{
103100
}
104101

105-
/// <summary>
106-
/// Proxy interface for <c>RouteData</c> class from Microsoft.AspNetCore.Routing.Abstractions
107-
/// </summary>
108-
public interface IRouteData
102+
/// <inheritdoc />
103+
public void OnNext(KeyValuePair<string, object> value)
104+
{
105+
if (value.Key == "Microsoft.AspNetCore.Mvc.BeforeAction")
106+
{
107+
var context = httpContextFetcher.Fetch(value.Value) as HttpContext;
108+
var routeData = routeDataFetcher.Fetch(value.Value);
109+
var routeValues = routeValuesFetcher.Fetch(routeData) as IDictionary<string, object>;
110+
111+
if (context != null && routeValues != null)
112+
{
113+
this.OnBeforeAction(context, routeValues);
114+
}
115+
}
116+
}
117+
118+
/// <inheritdoc />
119+
public void OnError(Exception error)
120+
{
121+
}
122+
123+
/// <inheritdoc />
124+
public void OnCompleted()
125+
{
126+
}
127+
128+
/// <inheritdoc />
129+
public void Dispose()
109130
{
110-
/// <summary>
111-
/// Gets the set of values produced by routes on the current routing path.
112-
/// </summary>
113-
IDictionary<string, object> Values { get; }
114131
}
115132
}
116133
}

0 commit comments

Comments
 (0)