forked from projectkudu/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraceServices.cs
102 lines (84 loc) · 2.72 KB
/
TraceServices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Diagnostics;
using System.Web;
using Kudu.Contracts.Tracing;
using Kudu.Core.Tracing;
namespace Kudu.Services.Web.Tracing
{
public static class TraceServices
{
private static readonly object _traceKey = new object();
private static readonly object _traceFileKey = new object();
private static Func<ITracer> _traceFactory;
internal static ITracer CurrentRequestTracer
{
get
{
var httpContext = HttpContext.Current;
if (httpContext == null)
{
return null;
}
return GetRequestTracer(httpContext);
}
}
internal static string CurrentRequestTraceFile
{
get
{
var httpContext = HttpContext.Current;
if (httpContext == null)
{
return null;
}
return httpContext.Items[_traceFileKey] as string;
}
}
internal static string HttpMethod
{
get
{
var httpContext = HttpContext.Current;
if (httpContext == null)
{
return null;
}
return httpContext.Request.HttpMethod;
}
}
internal static TraceLevel TraceLevel
{
get; set;
}
public static void SetTraceFactory(Func<ITracer> traceFactory)
{
_traceFactory = traceFactory;
}
internal static ITracer GetRequestTracer(HttpContext httpContext)
{
return httpContext.Items[_traceKey] as ITracer;
}
internal static void RemoveRequestTracer(HttpContext httpContext)
{
httpContext.Items.Remove(_traceKey);
httpContext.Items.Remove(_traceFileKey);
}
internal static ITracer EnsureETWTracer(HttpContext httpContext)
{
var etwTracer = new ETWTracer((string)httpContext.Items[Constants.RequestIdHeader], httpContext.Request.HttpMethod);
httpContext.Items[_traceKey] = etwTracer;
return etwTracer;
}
internal static ITracer CreateRequestTracer(HttpContext httpContext)
{
var tracer = (ITracer)httpContext.Items[_traceKey];
if (tracer == null)
{
httpContext.Items[_traceFileKey] = String.Format(Constants.TraceFileFormat, Environment.MachineName, Guid.NewGuid().ToString("D"));
tracer = _traceFactory();
httpContext.Items[_traceKey] = tracer;
}
return tracer;
}
}
}