-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (44 loc) · 1.86 KB
/
Copy pathProgram.cs
File metadata and controls
54 lines (44 loc) · 1.86 KB
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
using Microsoft.Diagnostics.Tracing.Session;
using System;
using System.Threading.Tasks;
namespace rolling_file_with_eventsource
{
public static class Program
{
private static readonly TimeSpan fileRollingInterval = TimeSpan.FromSeconds(10);
private const string TraceFileNamePrefix = "Trace.";
private const string TraceFileNameSuffix = ".etl";
private static int FileNumber = 1;
public async static Task Main(string[] args)
{
string fileName = GetNextFileName();
using TraceEventSession session = new TraceEventSession("Rolling-File-With-EventSource", fileName);
Console.WriteLine($"Setup session to write to {fileName}.");
// Register for CTRL+C.
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs cancelArgs)
{
session.Dispose();
Environment.Exit(0);
};
// Start writing events.
EventProducer.LogEvents();
// Start collecting events from the EventProducer.
session.EnableProvider("EventProducer", Microsoft.Diagnostics.Tracing.TraceEventLevel.Verbose, unchecked((ulong)-1));
do
{
// Wait while events are written to the current file.
Console.WriteLine($"Generating events for {fileRollingInterval.TotalSeconds} seconds.");
await Task.Delay(fileRollingInterval);
// Switch to the next file.
fileName = GetNextFileName();
session.SetFileName(fileName);
Console.WriteLine($"Rolled to new file {fileName}");
}
while (true);
}
private static string GetNextFileName()
{
return $"{TraceFileNamePrefix}{FileNumber++}{TraceFileNameSuffix}";
}
}
}