Skip to content

Commit 32a8162

Browse files
committed
Adding PostSharp.Samples.Logging.Etw.CustomSource.
1 parent 5405414 commit 32a8162

File tree

11 files changed

+381
-3
lines changed

11 files changed

+381
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<configuration>
4+
<startup>
5+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
6+
</startup>
7+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.Tracing;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using PostSharp.Patterns.Diagnostics;
9+
using PostSharp.Patterns.Diagnostics.Backends.EventSource;
10+
11+
namespace PostSharp.Samples.Logging.Etw.CustomSource
12+
{
13+
[Log(AttributeExclude = true)]
14+
[EventSource(Name = "MyEventSource")]
15+
[Guid("971d5f08-f366-421c-a25c-3aed379d5eb2")]
16+
class MyEventSource : PostSharpEventSource
17+
{
18+
private const string format = "{0} | {1} | {2} | {3}";
19+
private const string invalidOperationExceptionMessage = "Use the Log method. This method is metadata-only.";
20+
private const byte version = 2;
21+
22+
[Event(EventIds.MethodEntry, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Start, Version = version, Task = Tasks.Method)]
23+
internal void MethodEntry(string role, string type, string context, string message, int indentLevel)
24+
{
25+
throw new InvalidOperationException(invalidOperationExceptionMessage);
26+
}
27+
28+
29+
30+
[Event(EventIds.MethodSuccess, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Stop, Version = version, Task = Tasks.Method)]
31+
internal void MethodSuccess(string role, string type, string context, string message, int indentLevel)
32+
{
33+
throw new InvalidOperationException(invalidOperationExceptionMessage);
34+
}
35+
36+
37+
38+
[Event(EventIds.MethodException, Level = EventLevel.Warning, Message = format, Opcode = EventOpcode.Stop, Version = version, Task = Tasks.Method)]
39+
internal void MethodException(string role, string type, string context, string message, int indentLevel)
40+
{
41+
throw new InvalidOperationException(invalidOperationExceptionMessage);
42+
}
43+
44+
45+
46+
[Event(EventIds.MethodOvertime, Level = EventLevel.Warning, Message = format, Opcode = EventOpcode.Stop, Version = version, Task = Tasks.Method)]
47+
internal void MethodOvertime(string role, string type, string context, string message, int indentLevel)
48+
{
49+
throw new InvalidOperationException(invalidOperationExceptionMessage);
50+
}
51+
52+
53+
54+
[Event(EventIds.AsyncMethodAwait, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Suspend, Version = version, Task = Tasks.Method)]
55+
internal void AsyncMethodAwait(string role, string type, string context, string message, int indentLevel)
56+
{
57+
throw new InvalidOperationException(invalidOperationExceptionMessage);
58+
}
59+
60+
61+
62+
[Event(EventIds.AsyncMethodResume, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Resume, Version = version, Task = Tasks.Method)]
63+
internal void AsyncMethodResume(string role, string type, string context, string message, int indentLevel)
64+
{
65+
throw new InvalidOperationException(invalidOperationExceptionMessage);
66+
}
67+
68+
69+
70+
[Event(EventIds.ValueChanged, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Info, Version = version, Task = Tasks.Message)]
71+
internal void ValueChanged(string role, string type, string context, string message, int indentLevel)
72+
{
73+
throw new InvalidOperationException(invalidOperationExceptionMessage);
74+
}
75+
76+
77+
78+
[Event(EventIds.CustomVerbose, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Info, Version = version, Task = Tasks.Message)]
79+
internal void CustomVerbose(string role, string type, string context, string message, int indentLevel)
80+
{
81+
throw new InvalidOperationException(invalidOperationExceptionMessage);
82+
}
83+
84+
85+
86+
[Event(EventIds.CustomInfo, Level = EventLevel.Informational, Message = format, Opcode = EventOpcode.Info, Version = version, Task = Tasks.Message)]
87+
internal void CustomInfo(string role, string type, string context, string message, int indentLevel)
88+
{
89+
throw new InvalidOperationException(invalidOperationExceptionMessage);
90+
}
91+
92+
93+
94+
[Event(EventIds.CustomWarning, Level = EventLevel.Warning, Message = format, Opcode = EventOpcode.Info, Version = version, Task = Tasks.Message)]
95+
internal void CustomWarning(string role, string type, string context, string message, int indentLevel)
96+
{
97+
throw new InvalidOperationException(invalidOperationExceptionMessage);
98+
}
99+
100+
101+
102+
[Event(EventIds.CustomError, Level = EventLevel.Error, Message = format, Opcode = EventOpcode.Info, Version = version, Task = Tasks.Message)]
103+
internal void CustomError(string role, string type, string context, string message, int indentLevel)
104+
{
105+
throw new InvalidOperationException(invalidOperationExceptionMessage);
106+
}
107+
108+
109+
110+
[Event(EventIds.CustomCritical, Level = EventLevel.Critical, Message = format, Opcode = EventOpcode.Info, Version = version, Task = Tasks.Message)]
111+
internal void CustomCritical(string role, string type, string context, string message, int indentLevel)
112+
{
113+
throw new InvalidOperationException(invalidOperationExceptionMessage);
114+
}
115+
116+
117+
118+
[Event(EventIds.CustomActivityEntry, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Start, Version = version, Task = Tasks.CustomActivity)]
119+
internal void CustomActivityEntry(string role, string type, string context, string message, int indentLevel)
120+
{
121+
throw new InvalidOperationException(invalidOperationExceptionMessage);
122+
}
123+
124+
125+
126+
[Event(EventIds.CustomActivityException, Level = EventLevel.Warning, Message = format, Opcode = EventOpcode.Stop, Version = version, Task = Tasks.CustomActivity)]
127+
internal void CustomActivityException(string role, string type, string context, string message, int indentLevel)
128+
{
129+
throw new InvalidOperationException(invalidOperationExceptionMessage);
130+
}
131+
132+
133+
134+
[Event(EventIds.CustomActivitySuccess, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Stop, Version = version, Task = Tasks.CustomActivity)]
135+
internal void CustomActivitySuccess(string role, string type, string context, string message, int indentLevel)
136+
{
137+
throw new InvalidOperationException(invalidOperationExceptionMessage);
138+
}
139+
140+
141+
142+
[Event(EventIds.CustomActivityFailure, Level = EventLevel.Warning, Message = format, Opcode = EventOpcode.Stop, Version = version, Task = Tasks.CustomActivity)]
143+
internal void CustomActivityFailure(string role, string type, string context, string message, int indentLevel)
144+
{
145+
throw new InvalidOperationException(invalidOperationExceptionMessage);
146+
}
147+
148+
149+
150+
[Event(EventIds.IteratorYield, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Stop, Version = version, Task = Tasks.Method)]
151+
internal void IteratorYield(string role, string type, string context, string message, int indentLevel)
152+
{
153+
throw new InvalidOperationException(invalidOperationExceptionMessage);
154+
}
155+
156+
157+
158+
[Event(EventIds.IteratorMoveNext, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Start, Version = version, Task = Tasks.Method)]
159+
internal void IteratorMoveNext(string role, string type, string context, string message, int indentLevel)
160+
{
161+
throw new InvalidOperationException(invalidOperationExceptionMessage);
162+
}
163+
164+
165+
166+
[Event(EventIds.ExecutionPoint, Level = EventLevel.Verbose, Message = format, Opcode = EventOpcode.Info, Version = version, Task = Tasks.Method)]
167+
internal void ExecutionPoint(string role, string type, string context, string message, int indentLevel)
168+
{
169+
throw new InvalidOperationException(invalidOperationExceptionMessage);
170+
}
171+
172+
}
173+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\..\packages\PostSharp.5.0.43\build\PostSharp.props" Condition="Exists('..\..\packages\PostSharp.5.0.43\build\PostSharp.props')" />
4+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
5+
<PropertyGroup>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{637CB8F6-4F80-4D14-B1A6-2095A2A743C9}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<RootNamespace>PostSharp.Samples.Logging.Etw.CustomSource</RootNamespace>
11+
<AssemblyName>PostSharp.Samples.Logging.Etw.CustomSource</AssemblyName>
12+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<NuGetPackageImportStamp>
16+
</NuGetPackageImportStamp>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19+
<PlatformTarget>AnyCPU</PlatformTarget>
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<PlatformTarget>AnyCPU</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="PostSharp, Version=5.0.43.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7">
39+
<HintPath>..\..\packages\PostSharp.Redist.5.0.43\lib\net45\PostSharp.dll</HintPath>
40+
<Private>True</Private>
41+
</Reference>
42+
<Reference Include="PostSharp.Patterns.Common, Version=5.0.43.0, Culture=neutral, PublicKeyToken=e7f631e6ce13f078">
43+
<HintPath>..\..\packages\PostSharp.Patterns.Common.Redist.5.0.43\lib\net46\PostSharp.Patterns.Common.dll</HintPath>
44+
<Private>True</Private>
45+
</Reference>
46+
<Reference Include="PostSharp.Patterns.Diagnostics, Version=5.0.43.0, Culture=neutral, PublicKeyToken=e7f631e6ce13f078">
47+
<HintPath>..\..\packages\PostSharp.Patterns.Diagnostics.Redist.5.0.43\lib\net46\PostSharp.Patterns.Diagnostics.dll</HintPath>
48+
<Private>True</Private>
49+
</Reference>
50+
<Reference Include="PostSharp.Patterns.Diagnostics.Backends.Tracing, Version=5.0.43.0, Culture=neutral, PublicKeyToken=e7f631e6ce13f078">
51+
<HintPath>..\..\packages\PostSharp.Patterns.Diagnostics.Tracing.5.0.43\lib\net46\PostSharp.Patterns.Diagnostics.Backends.Tracing.dll</HintPath>
52+
<Private>True</Private>
53+
</Reference>
54+
<Reference Include="System" />
55+
<Reference Include="System.Core" />
56+
<Reference Include="System.Xml.Linq" />
57+
<Reference Include="System.Data.DataSetExtensions" />
58+
<Reference Include="Microsoft.CSharp" />
59+
<Reference Include="System.Data" />
60+
<Reference Include="System.Net.Http" />
61+
<Reference Include="System.Xml" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<Compile Include="MyEventSource.cs" />
65+
<Compile Include="Program.cs" />
66+
<Compile Include="Properties\AssemblyInfo.cs" />
67+
</ItemGroup>
68+
<ItemGroup>
69+
<None Include="App.config" />
70+
<None Include="packages.config" />
71+
<None Include="README.md" />
72+
</ItemGroup>
73+
<Import Project="..\PostSharp.Samples.Logging.BusinessLogic\PostSharp.Samples.Logging.BusinessLogic.projitems" Label="Shared" />
74+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
75+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
76+
<PropertyGroup>
77+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
78+
</PropertyGroup>
79+
<Error Condition="!Exists('..\..\packages\PostSharp.5.0.43\build\PostSharp.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.5.0.43\build\PostSharp.props'))" />
80+
<Error Condition="!Exists('..\..\packages\PostSharp.5.0.43\build\PostSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.5.0.43\build\PostSharp.targets'))" />
81+
</Target>
82+
<Import Project="..\..\packages\PostSharp.5.0.43\build\PostSharp.targets" Condition="Exists('..\..\packages\PostSharp.5.0.43\build\PostSharp.targets')" />
83+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using PostSharp.Patterns.Diagnostics;
2+
using PostSharp.Patterns.Diagnostics.Backends.EventSource;
3+
using PostSharp.Samples.Logging.BusinessLogic;
4+
5+
// Add logging to all methods of this project.
6+
[assembly: Log]
7+
8+
namespace PostSharp.Samples.Logging.Etw.CustomSource
9+
{
10+
[Log(AttributeExclude = true)] // Removes logging from the Program class itself.
11+
internal class Program
12+
{
13+
private static void Main(string[] args)
14+
{
15+
var eventSourceBackend = new EventSourceLoggingBackend(new MyEventSource());
16+
if (eventSourceBackend.EventSource.ConstructionException != null)
17+
throw eventSourceBackend.EventSource.ConstructionException;
18+
19+
LoggingServices.DefaultBackend = eventSourceBackend;
20+
21+
// Simulate some business logic.
22+
QueueProcessor.ProcessQueue(@".\Private$\SyncRequestQueue");
23+
24+
// To collect and view the log:
25+
// 1. Download PerfView from https://www.microsoft.com/en-us/download/details.aspx?id=28567.
26+
// 2. Execute: perfview.exe collect -OnlyProviders:*MyEventSource
27+
// 3. Execute this program.
28+
// 4. In PerfView, click 'Stop collecting', then in the PerfView tree view click 'PerfViewData.etl.zip' and finally 'Events'.
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("PostSharp.Samples.Logging.Etw")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("PostSharp.Samples.Logging.Etw")]
12+
[assembly: AssemblyCopyright("Copyright © 2017")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("97830c73-1d7f-48a4-92f0-0beabc8216db")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
This example demonstrates how to configure customize the name and GUID of the ETW event source.
2+
3+
The custom log source is implemented in the `MyEventSource` class. This class must fulfill the following requirements:
4+
5+
* Inherit the `PostSharpEventSource` class
6+
* Be annotated with the `EventSource` and `Guid` custom attributes.
7+
* Contain a set of predefined methods with arbitrary name but with predefined signature and with an `Event` custom attribute
8+
with the exact predefined value of the `eventId` parameter. These methods exist for the sole purpose of defining the
9+
event metadata. They are never invoked directly.
10+
11+
12+
An instance of the new event source class is passed to the constructor of `EventSourceLoggingBackend`.
13+
14+
15+
## Viewing the log
16+
17+
To view the log produced by this example, you need to attach a log collector:
18+
19+
1. Download PerfView from https://www.microsoft.com/en-us/download/details.aspx?id=28567.
20+
21+
2. Execute: perfview.exe collect -OnlyProviders:*MyEventSource
22+
23+
3. Execute this program.
24+
25+
4. In PerfView, click 'Stop collecting', then in the PerfView tree view click 'PerfViewData.etl.zip' and finally 'Events'.
26+
27+
28+
## Documentation
29+
30+
[Logging to the etw](http://doc.postsharp.net/etw)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="PostSharp" version="5.0.43" targetFramework="net46" developmentDependency="true" />
4+
<package id="PostSharp.Patterns.Common" version="5.0.43" targetFramework="net46" developmentDependency="true" />
5+
<package id="PostSharp.Patterns.Common.Redist" version="5.0.43" targetFramework="net46" />
6+
<package id="PostSharp.Patterns.Diagnostics" version="5.0.43" targetFramework="net46" developmentDependency="true" />
7+
<package id="PostSharp.Patterns.Diagnostics.Redist" version="5.0.43" targetFramework="net46" />
8+
<package id="PostSharp.Patterns.Diagnostics.Tracing" version="5.0.43" targetFramework="net46" />
9+
<package id="PostSharp.Redist" version="5.0.43" targetFramework="net46" />
10+
</packages>

Diagnostics/PostSharp.Samples.Logging.Etw/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private static void Main(string[] args)
2323

2424
// To collect and view the log:
2525
// 1. Download PerfView from https://www.microsoft.com/en-us/download/details.aspx?id=28567.
26-
// 2. Execute: perfview.exe collect -OnlyProviders *PostSharp-Patterns-Diagnostics
26+
// 2. Execute: perfview.exe collect -OnlyProviders:*PostSharp-Patterns-Diagnostics
2727
// 3. Execute this program.
2828
// 4. In PerfView, click 'Stop collecting', then in the PerfView tree view click 'PerfViewData.etl.zip' and finally 'Events'.
2929
}

Diagnostics/PostSharp.Samples.Logging.Etw/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ To view the log produced by this example, you need to attach a log collector:
1616

1717
1. Download PerfView from https://www.microsoft.com/en-us/download/details.aspx?id=28567.
1818

19-
2. Execute: perfview.exe collect -OnlyProviders *PostSharp-Patterns-Diagnostics
19+
2. Execute: perfview.exe collect -OnlyProviders:*PostSharp-Patterns-Diagnostics
2020

2121
3. Execute this program.
2222

0 commit comments

Comments
 (0)