forked from microsoft/referencesource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelemetryLogger.cs
111 lines (95 loc) · 3.78 KB
/
TelemetryLogger.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
103
104
105
106
107
108
109
110
111
//------------------------------------------------------------------------------
// <copyright file="TelemetryLogger.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web {
using System.Diagnostics.Tracing;
using System.Security.Cryptography;
using System.Text;
using System.Web.Util;
[EventData(Name="TargetFrameworkSet")]
struct TargetFrameworkTelemetryData {
public string AppID { get; set; }
public string TargetFramework { get; set; }
}
[EventData(Name="HandlerMapped")]
struct HttpHandlerTelemetryData {
public string AppID { get; set; }
public string HttpHandlerType { get; set; }
}
[EventData(Name = "ProviderInitialized")]
struct ProviderTelemetryData {
public string AppID { get; set; }
public string ProviderType { get; set; }
}
static class TelemetryLogger {
// eventsource provider name
private static readonly string WebFormsProviderName = "Microsoft.DOTNET.ASPNET.WebForms";
// event name
private static readonly string HttpHandlerEventName = "HandlerMapped";
private static readonly string TargetFrameworkEventName = "TargetFrameworkSet";
private static readonly string ProviderEventName = "ProviderInitialized";
// telemetry eventsource
private static EventSource s_TelemetryLogger = new TelemetryEventSource(WebFormsProviderName);
private static readonly string s_AppID = GetAppID();
public static void LogHttpHandler(Type httpHandlerType) {
if (httpHandlerType == null) {
return;
}
try {
s_TelemetryLogger.Write(
HttpHandlerEventName,
TelemetryEventSource.MeasuresOptions(),
new HttpHandlerTelemetryData() {
AppID = s_AppID,
HttpHandlerType = GetHashCode(httpHandlerType.AssemblyQualifiedName)
}
);
}
catch { }
}
public static void LogTargetFramework(Version targetFrameworkVersion) {
if (targetFrameworkVersion == null) {
return;
}
try {
s_TelemetryLogger.Write(
TargetFrameworkEventName,
TelemetryEventSource.MeasuresOptions(),
new TargetFrameworkTelemetryData() {
AppID = s_AppID,
TargetFramework = targetFrameworkVersion.ToString()
}
);
}
catch { }
}
public static void LogProvider(Type providerType) {
if (providerType == null) {
return;
}
try {
s_TelemetryLogger.Write(
ProviderEventName,
TelemetryEventSource.MeasuresOptions(),
new ProviderTelemetryData() {
AppID = s_AppID,
ProviderType = GetHashCode(providerType.AssemblyQualifiedName)
}
);
}
catch { }
}
private static string GetAppID() {
return HttpRuntime.AppDomainAppId == null ? string.Empty : GetHashCode(HttpRuntime.AppDomainAppId);
}
private static string GetHashCode(string str) {
Debug.Assert(str != null);
var bytes = Encoding.Unicode.GetBytes(str);
using(var sha256 = new SHA256Managed()) {
return Convert.ToBase64String(sha256.ComputeHash(bytes));
}
}
}
}