Skip to content

Commit 184cfbb

Browse files
committed
Update and documentation of PostSharp.Samples.Logging.ElasticStack.
1 parent 102f323 commit 184cfbb

8 files changed

Lines changed: 122 additions & 7 deletions

File tree

Diagnostics/PostSharp.Samples.Logging.ElasticStack/ClientExample/ClientExample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
</ItemGroup>
2121

2222
<ItemGroup Condition="'$(Configuration)'!='Development'">
23-
<PackageReference Include="PostSharp.Patterns.Diagnostics" Version="6.1.6-preview" />
24-
<PackageReference Include="PostSharp.Patterns.Diagnostics.Serilog" Version="6.1.6-preview" />
23+
<PackageReference Include="PostSharp.Patterns.Diagnostics" Version="6.1.18" />
24+
<PackageReference Include="PostSharp.Patterns.Diagnostics.Serilog" Version="6.1.18" />
2525
</ItemGroup>
2626

2727
<ItemGroup Condition="'$(Configuration)'=='Development'">
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# PostSharp.Samples.Logging.ElasticStack: ClientExample
2+
3+
This is the client side of the PostSharp.Samples.Logging.ElasticStack example.
4+
5+
The *InstrumentOutgoingRequestsAspect* aspect is the most interesting part of this project. It instruments the HttpClient class. It basically does three things:
6+
7+
* Open a client-side activity (aka context), which emits log client-side records into Elastic Search.
8+
* Set the `Request-Id` header of the outgoing HTTP request to the `SyntheticId` of the newly created activity.
9+
* Set the `Correlation-Context` header ot the outgoing HTTP request to the baggage (i.e. cross-process properties). In this example, we've set a cross-process property named `User` in `Program.Main`.

Diagnostics/PostSharp.Samples.Logging.ElasticStack/MicroserviceExample/MicroserviceExample.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1" />
19+
<PackageReference Include="Microsoft.AspNetCore.App" />
2020
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
2121
<PackageReference Include="Serilog.Sinks.Async" Version="1.3.0" />
2222
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
@@ -25,8 +25,8 @@
2525

2626

2727
<ItemGroup Condition="'$(Configuration)'!='Development'">
28-
<PackageReference Include="PostSharp.Patterns.Diagnostics" Version="6.1.6-preview" />
29-
<PackageReference Include="PostSharp.Patterns.Diagnostics.Serilog" Version="6.1.6-preview" />
28+
<PackageReference Include="PostSharp.Patterns.Diagnostics" Version="6.1.18" />
29+
<PackageReference Include="PostSharp.Patterns.Diagnostics.Serilog" Version="6.1.18" />
3030
</ItemGroup>
3131

3232
<ItemGroup Condition="'$(Configuration)'=='Development'">

Diagnostics/PostSharp.Samples.Logging.ElasticStack/MicroserviceExample/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public static void Main( string[] args )
4141
LoggingServices.Formatters.Register(new ActionResultFormatter());
4242
LoggingServices.Formatters.Register(new ObjectResultFormatter());
4343

44-
// SampledLoggingFilter.Initialize(backend);
45-
// LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel(LogLevel.Warning);
44+
// Log only warnings by default, except for 10% randomly chosen requests.
45+
SampledLoggingActionFilter.Initialize(backend);
46+
LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel(LogLevel.Warning);
4647

4748

4849

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# PostSharp.Samples.Logging.ElasticStack: MicroserviceExample
2+
3+
This is the server side of the PostSharp.Samples.Logging.ElasticStack example.
4+
5+
There are two interesting artifacts in this project:
6+
7+
* *LoggingActionFilter* is an ASP.NET Action Filter, i.e. it runs before the control flow is given to your code in the controller classes. This action filter does the "opposite" of the *InstrumentOutgoingRequestsAspect* server-side aspect, i.e. it creates a new server-side logging activity (aka context) and assigns:
8+
9+
* the `SyntheticParentId` property to the value of the `Request-Id` HTTP header sent by the client, and
10+
* custom properties based on the parsing of the `Correlation-Context` HTTP header.
11+
12+
* *SampledLoggingActionFilter* is another ASP.NET Action Filter which enables logging for a random 10% subset of incoming requests. The default level, for the remaining 90% of requests, is set to Warning in `Program.Main`.

Diagnostics/PostSharp.Samples.Logging.ElasticStack/MicroserviceExample/SampledLoggingActionFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class SampledLoggingActionFilter : IAsyncActionFilter
1414
public static void Initialize( LoggingBackend backend )
1515
{
1616
verbosityManager = backend.CreateVerbosityConfiguration();
17+
// Verbosity is High (Debug level) by default.
1718
}
1819

1920
public static bool IsInitialized => verbosityManager != null;

PostSharp.Samples.sln

Lines changed: 89 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ or you can download them on [GitHub](https://www.github.com/postsharp/PostSharp.
3737
| [PostSharp.Samples.Logging.CustomBackend.ServiceStack](Diagnostics/PostSharp.Samples.Logging.CustomBackend.ServiceStack/) | Demonstrates how to implement a PostSharp Logging adapter for your custom logging framework. |
3838
| [PostSharp.Samples.Logging.Audit](Diagnostics/PostSharp.Samples.Audit/) | Shows how to append an audit record to a database when a method is invoked. |
3939
| [PostSharp.Samples.Logging.Audit.Extended](Diagnostics/PostSharp.Samples.Audit.Extended/) | Shows how to add custom pieces of information to the audit record. |
40+
| **Distributed Diagnostics with Elastic Search**
41+
| [PostSharp.Samples.Logging.ElasticStack/MicroserviceExample](Diagnostics/PostSharp.Samples.Logging.ElasticStack/MicroserviceExample/) | The server-side service demonstrating correlation of logs of a distributed application with Elastic Search. |
42+
| [PostSharp.Samples.Logging.ElasticStack/ClientExample](Diagnostics/PostSharp.Samples.Logging.ElasticStack/ClientExample/) | The server-side service demonstrating correlation of logs of a distributed application with Elastic Search. |
4043
| **XAML**
4144
| [PostSharp.Samples.Xaml](Xaml/PostSharp.Samples.Xaml/) | Demonstrates a few ready-made aspects that are useful for XAML |
4245
| **Caching**

0 commit comments

Comments
 (0)