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

Commit 419327e

Browse files
committed
Added EventSource to Hosting
1 parent 48ca10c commit 419327e

File tree

4 files changed

+386
-0
lines changed

4 files changed

+386
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public Context CreateContext(IFeatureCollection contextFeatures)
4343
_diagnosticSource.Write("Microsoft.AspNetCore.Hosting.BeginRequest", new { httpContext = httpContext, timestamp = startTimestamp });
4444
}
4545

46+
HostingEventSource.Log.RequestStart(httpContext);
47+
4648
return new Context
4749
{
4850
HttpContext = httpContext,
@@ -66,6 +68,8 @@ public void DisposeContext(Context context, Exception exception)
6668
{
6769
_diagnosticSource.Write("Microsoft.AspNetCore.Hosting.EndRequest", new { httpContext = httpContext, timestamp = currentTimestamp });
6870
}
71+
72+
HostingEventSource.Log.RequestStop(httpContext);
6973
}
7074
else
7175
{
@@ -78,6 +82,8 @@ public void DisposeContext(Context context, Exception exception)
7882
{
7983
_diagnosticSource.Write("Microsoft.AspNetCore.Hosting.UnhandledException", new { httpContext = httpContext, timestamp = currentTimestamp, exception = exception });
8084
}
85+
86+
HostingEventSource.Log.RequestStop(httpContext, exception);
8187
}
8288

8389
context.Scope?.Dispose();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.AspNetCore.Http;
7+
8+
namespace Microsoft.AspNetCore.Hosting.Internal
9+
{
10+
[EventSource(Name = "Microsoft-AspNetCore-Hosting")]
11+
public sealed class HostingEventSource : EventSource
12+
{
13+
public static readonly HostingEventSource Log = new HostingEventSource();
14+
15+
private HostingEventSource() { }
16+
17+
[Event(1, Level = EventLevel.Informational)]
18+
public void HostStart()
19+
{
20+
WriteEvent(1);
21+
}
22+
23+
[Event(2, Level = EventLevel.Informational)]
24+
public void HostStop()
25+
{
26+
WriteEvent(2);
27+
}
28+
29+
[NonEvent]
30+
public void RequestStart(HttpContext context)
31+
{
32+
if (IsEnabled())
33+
{
34+
RequestStart(
35+
context.TraceIdentifier,
36+
context.Request.Protocol,
37+
context.Request.Method,
38+
context.Request.ContentType ?? string.Empty,
39+
context.Request.ContentLength.HasValue ? context.Request.ContentLength.Value.ToString() : string.Empty,
40+
context.Request.Scheme,
41+
context.Request.Host.ToString(),
42+
context.Request.PathBase,
43+
context.Request.Path,
44+
context.Request.QueryString.ToString());
45+
}
46+
}
47+
48+
[NonEvent]
49+
public void RequestStop(HttpContext context, Exception exception = null)
50+
{
51+
if (IsEnabled())
52+
{
53+
RequestStop(
54+
context.Response.StatusCode,
55+
context.Response.ContentType ?? string.Empty,
56+
context.TraceIdentifier,
57+
exception == null ? string.Empty : exception.ToString());
58+
}
59+
}
60+
61+
[Event(3, Level = EventLevel.Informational)]
62+
public void RequestStart(
63+
string requestTraceIdentifier,
64+
string protocol,
65+
string method,
66+
string contentType,
67+
string contentLength,
68+
string scheme,
69+
string host,
70+
string pathBase,
71+
string path,
72+
string queryString)
73+
{
74+
WriteEvent(
75+
3,
76+
requestTraceIdentifier,
77+
protocol,
78+
method,
79+
contentType,
80+
contentLength,
81+
scheme,
82+
host,
83+
pathBase,
84+
path,
85+
queryString);
86+
}
87+
88+
[Event(4, Level = EventLevel.Informational)]
89+
private void RequestStop(int statusCode, string contentType, string requestTraceIdentifier, string exception)
90+
{
91+
WriteEvent(4, statusCode, contentType, requestTraceIdentifier, exception);
92+
}
93+
}
94+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public void Initialize()
9999

100100
public virtual void Start()
101101
{
102+
HostingEventSource.Log.HostStart();
103+
102104
Initialize();
103105

104106
_logger = _applicationServices.GetRequiredService<ILogger<WebHost>>();
@@ -248,6 +250,8 @@ public void Dispose()
248250
(_hostingServiceProvider as IDisposable)?.Dispose();
249251
(_applicationServices as IDisposable)?.Dispose();
250252
_applicationLifetime.NotifyStopped();
253+
254+
HostingEventSource.Log.HostStop();
251255
}
252256
}
253257
}

0 commit comments

Comments
 (0)