Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 47ceec2

Browse files
committed
Added event counters
1 parent 3c3d4b8 commit 47ceec2

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

src/Microsoft.AspNetCore.Hosting/Internal/HostingApplication.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public Context CreateContext(IFeatureCollection contextFeatures)
3434
{
3535
var httpContext = _httpContextFactory.Create(contextFeatures);
3636
var diagnoticsEnabled = _diagnosticSource.IsEnabled("Microsoft.AspNetCore.Hosting.BeginRequest");
37-
var startTimestamp = (diagnoticsEnabled || _logger.IsEnabled(LogLevel.Information)) ? Stopwatch.GetTimestamp() : 0;
37+
var startTimestamp = (diagnoticsEnabled
38+
|| _logger.IsEnabled(LogLevel.Information)
39+
|| HostingEventSource.Log.IsEnabled()) ? Stopwatch.GetTimestamp() : 0;
3840

3941
var scope = _logger.RequestScope(httpContext);
4042
_logger.RequestStarting(httpContext);
@@ -68,8 +70,7 @@ public void DisposeContext(Context context, Exception exception)
6870
{
6971
_diagnosticSource.Write("Microsoft.AspNetCore.Hosting.EndRequest", new { httpContext = httpContext, timestamp = currentTimestamp });
7072
}
71-
72-
HostingEventSource.Log.RequestStop(httpContext);
73+
HostingEventSource.Log.RequestStop(httpContext, context.StartTimestamp, currentTimestamp);
7374
}
7475
else
7576
{
@@ -82,8 +83,7 @@ public void DisposeContext(Context context, Exception exception)
8283
{
8384
_diagnosticSource.Write("Microsoft.AspNetCore.Hosting.UnhandledException", new { httpContext = httpContext, timestamp = currentTimestamp, exception = exception });
8485
}
85-
86-
HostingEventSource.Log.RequestStop(httpContext, exception);
86+
HostingEventSource.Log.RequestStop(httpContext, context.StartTimestamp, currentTimestamp, exception);
8787
}
8888

8989
context.Scope?.Dispose();

src/Microsoft.AspNetCore.Hosting/Internal/HostingEventSource.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ public sealed class HostingEventSource : EventSource
1212
{
1313
public static readonly HostingEventSource Log = new HostingEventSource();
1414

15-
private HostingEventSource() { }
15+
#if NETSTANDARD1_5
16+
private EventCounter _requestCounter = null;
17+
private EventCounter _successfulRequestCounter = null;
18+
private EventCounter _failedRequestCounter = null;
19+
private EventCounter _requestExecutionTimeCounter = null;
20+
#endif
21+
22+
private HostingEventSource()
23+
{
24+
#if NETSTANDARD1_5
25+
_requestCounter = new EventCounter("Request", this);
26+
_successfulRequestCounter = new EventCounter("SuccessfulRequest", this);
27+
_failedRequestCounter = new EventCounter("FailedRequest", this);
28+
_requestExecutionTimeCounter = new EventCounter("RequestExecutionTime", this);
29+
#endif
30+
}
1631

1732
[Event(1, Level = EventLevel.Informational)]
1833
public void HostStart()
@@ -29,6 +44,9 @@ public void HostStop()
2944
[NonEvent]
3045
public void RequestStart(HttpContext context)
3146
{
47+
#if NETSTANDARD1_5
48+
_requestCounter.WriteMetric(1);
49+
#endif
3250
if (IsEnabled())
3351
{
3452
RequestStart(
@@ -46,8 +64,23 @@ public void RequestStart(HttpContext context)
4664
}
4765

4866
[NonEvent]
49-
public void RequestStop(HttpContext context, Exception exception = null)
67+
public void RequestStop(HttpContext context, long startTimestamp, long endTimestamp, Exception exception = null)
5068
{
69+
#if NETSTANDARD1_5
70+
if (exception == null)
71+
{
72+
_successfulRequestCounter.WriteMetric(1);
73+
}
74+
else
75+
{
76+
_failedRequestCounter.WriteMetric(1);
77+
}
78+
79+
if (endTimestamp != 0)
80+
{
81+
_requestExecutionTimeCounter.WriteMetric(endTimestamp - startTimestamp);
82+
}
83+
#endif
5184
if (IsEnabled())
5285
{
5386
RequestStop(

src/Microsoft.AspNetCore.Hosting/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"netstandard1.5": {
7878
"dependencies": {
7979
"System.Diagnostics.StackTrace": "4.3.0-*",
80+
"System.Diagnostics.Tracing": "4.3.0-*",
8081
"System.Runtime.Loader": "4.3.0-*"
8182
}
8283
}

0 commit comments

Comments
 (0)