-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed8c240
commit 426eefc
Showing
10 changed files
with
166 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Tracing; | ||
using System.Globalization; | ||
using System.IO; | ||
using Microsoft.Diagnostics.Tracing.Session; | ||
using Microsoft.PowerToys.Telemetry; | ||
|
||
namespace WSACrashHelper | ||
{ | ||
/// <summary> | ||
/// This class is based loosely on the C++ ETWTrace class in Win32client/Framework project. | ||
/// It is intended to record telemetry events generated by the WSACrashUploader so that end users | ||
/// can view them if they want. | ||
/// </summary> | ||
public class ETWTrace : IDisposable | ||
{ | ||
internal const EventKeywords TelemetryKeyword = (EventKeywords)0x0000200000000000; | ||
internal const EventKeywords MeasuresKeyword = (EventKeywords)0x0000400000000000; | ||
internal const EventKeywords CriticalDataKeyword = (EventKeywords)0x0000800000000000; | ||
|
||
private readonly bool telemetryEnabled = DataDiagnosticsSettings.GetEnabledValue(); // This is the global telemetry setting on whether to log events | ||
private readonly bool telemetryRecordingEnabled = DataDiagnosticsSettings.GetViewEnabledValue(); // This is the setting for recording telemetry events to disk for vewng | ||
private readonly string etwFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\PowerToys\", "etw"); | ||
private bool disposedValue; | ||
private string sessionName; | ||
private string etwFilePath; | ||
private bool started; | ||
#nullable enable | ||
private TraceEventSession? traceSession; | ||
#nullable disable | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ETWTrace"/> class. | ||
/// </summary> | ||
public ETWTrace() | ||
{ | ||
if (File.Exists(etwFolderPath)) | ||
{ | ||
File.Delete(etwFolderPath); | ||
} | ||
|
||
if (!Directory.Exists(etwFolderPath)) | ||
{ | ||
Directory.CreateDirectory(etwFolderPath); | ||
} | ||
|
||
if (this.telemetryEnabled && this.telemetryRecordingEnabled) | ||
{ | ||
this.Start(); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
this.Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Starts the trace session. | ||
/// </summary> | ||
public void Start() | ||
{ | ||
lock (this) | ||
{ | ||
if (this.started) | ||
{ | ||
return; | ||
} | ||
|
||
string executable = Process.GetCurrentProcess().ProcessName; | ||
string dateTimeNow = DateTime.Now.ToString("MM-d-yyy__H_mm_ss", CultureInfo.InvariantCulture); | ||
this.sessionName = string.Format(CultureInfo.InvariantCulture, "{0}-{1}-{2}", executable, Environment.ProcessId, dateTimeNow); | ||
this.etwFilePath = Path.Combine(etwFolderPath, $"{this.sessionName}.etl"); | ||
this.traceSession = new TraceEventSession( | ||
this.sessionName, this.etwFilePath, (TraceEventSessionOptions)(1 | 0x10 | 0x20)); | ||
this.traceSession.EnableProvider( | ||
PowerToysTelemetry.Log.Guid, | ||
matchAnyKeywords: (ulong)TelemetryKeyword | (ulong)MeasuresKeyword | (ulong)CriticalDataKeyword); | ||
|
||
this.started = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Stops the trace session. | ||
/// </summary> | ||
public void Stop() | ||
{ | ||
lock (this) | ||
{ | ||
if (!this.started) | ||
{ | ||
return; | ||
} | ||
|
||
if (this.traceSession != null) | ||
{ | ||
Trace.TraceInformation("Disposing EventTraceSession"); | ||
this.traceSession.Dispose(); | ||
this.traceSession = null; | ||
this.started = false; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the object. | ||
/// </summary> | ||
/// <param name="disposing">boolean for disposing.</param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!this.disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
this.Stop(); | ||
} | ||
|
||
this.disposedValue = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.