You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the clientside of the PostSharp.Samples.Logging.ElasticStack example.
3
+
This is the client-side of the PostSharp.Samples.Logging.ElasticStack example.
4
4
5
-
The *InstrumentOutgoingRequestsAspect* aspect is the most interesting part of this project. It instruments the HttpClient class. It basically does three things:
5
+
A few interesting points:
6
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`.
7
+
* Serilog is configured to write to the console and to Elastic Search (actually logstash).
8
+
* PostSharp Logging is configured to write to Serilog.
9
+
* The call to `HttpClientLogging.Initialize()` adds logging to outgoing HTTP requests, but also adds the headers that are required for distributed logging.
10
+
* The top-level custom attribute `[assembly: Log]` adds logging to every single method in the current project.
This is the serverside of the PostSharp.Samples.Logging.ElasticStack example.
3
+
This is the server-side of the PostSharp.Samples.Logging.ElasticStack example.
4
4
5
-
There are two interesting artifacts in this project:
5
+
A few interesting points:
6
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`.
7
+
* Serilog is configured to write to the console and to Elastic Search (actually logstash).
8
+
* PostSharp Logging is configured to write to Serilog.
9
+
* The call to `AspNetCoreLogging.Initialize()` adds logging to the ASP.NET Core stack.
10
+
* The top-level custom attribute `[assembly: Log]` adds logging to every single method in the current project.
Copy file name to clipboardExpand all lines: Diagnostics/PostSharp.Samples.Logging.ElasticStack/README.md
+32-12Lines changed: 32 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Serilog and the Elastic Stack
1
+
# PostSharp Logging, Serilog and the Elastic Stack
2
2
3
3
## Table of contents
4
4
@@ -13,33 +13,52 @@
13
13
14
14
## Credits
15
15
16
-
This example is forked from https://github.com/FantasticFiasco/serilog-sinks-http-sample-elastic-stack. All credits for integrating Elastic Stack with Serilog go to the original authors.
16
+
This example is forked from https://github.com/FantasticFiasco/serilog-sinks-http-sample-elastic-stack.
17
+
All credits for integrating Elastic Stack with Serilog go to the original authors.
17
18
18
-
The `elastic-stack` directory is a clone of [docker-elk](https://github.com/deviantony/docker-elk) with minor modifications done by FantasticFiasco. Credit to [deviantony](https://github.com/deviantony) for publishing the Elastic Stack boilerplate.
19
+
The `elastic-stack` directory is a clone of [docker-elk](https://github.com/deviantony/docker-elk) with minor modifications done by FantasticFiasco.
20
+
Credit to [deviantony](https://github.com/deviantony) for publishing the Elastic Stack boilerplate.
19
21
20
22
## Introduction
21
23
22
-
[Elastic Stack](https://www.elastic.co/products) is fantastic at collecting and visualizing log events. [Serilog](https://serilog.net/) is fantastic at producing structured log events. [PostSharp Diagnostics](https://www.postsharp.net/diagnostics)
23
-
is fantastic at instrumenting your project and feeding Serilog with plently structured log events.
24
+
This example shows how to combine PostSharp Logging, Serilog and the Elastic Stack to achieve logging of a distributed application:
24
25
25
-
This repository provides a sandbox where developers can explore the life of a log event starting with its birth in PostSharp and Serilog, its transport over the network to Logstash, its fields being indexed by Elasticsearch and finally its legacy being recorded as a historical event in Kibana.
26
+
*[Elastic Stack](https://www.elastic.co/products) is fantastic at collecting and visualizing log events.
27
+
*[Serilog](https://serilog.net/) is fantastic at producing structured log events.
28
+
*[PostSharp Logging](https://www.postsharp.net/logging) is fantastic at instrumenting your project and feeding Serilog with plently structured log events.
29
+
30
+
This repository provides a sandbox where developers can explore the life of a log event starting with its birth in PostSharp and Serilog,
31
+
its transport over the network to Logstash, its fields being indexed by Elasticsearch and finally its legacy being recorded as a historical event in Kibana.
26
32
27
33
This example contains two applications that run together, so we can explore the correlation of requests coming from both apps.
28
34
35
+
PostSharp Logging and Serilog are initialized in both applications in `Program.Main`.
36
+
29
37
Correlation is implemented by two artifacts:
30
38
31
-
* On client-side, `InstrumentOutgoingRequestsAspect`, a PostSharp aspect, adds a header to `HttpClient`.
39
+
* On client-side, a call to `HttpClientLogging.Initialize()`, which intercepts calls to `HttpClient` (outgoing HTTP requests) and adds correlation headers.
32
40
33
-
* On server-side, `LoggingActionFilter`, an ASP.NET Action Filter, reads the header and adds it as a logging property.
41
+
* On server-side, a call to `AspNetCoreLogging.Initialize()`, which intercepts incoming HTTP requests and interprets the correlation headers.
42
+
43
+
If you have an application that acts both as an HTTP client and server, you would need both calls.
* Automatic logging without boilerplate: your code is completely instrumented and you don't by a just a couple of custom attributes.
51
+
52
+
* EventId: a synthetic id, cross-process identifier that makes easy to get all traces of a single distributed transaction just by using
53
+
the `StartsWith` operator. For instance, in Kibana, try a filter like `fields.EventId: '4b6e6bfaaa.a2.a3.b33.a7*'`.
54
+
55
+
* User: a baggage property, defined in the client and transported to the server. Try to display `fields.#User`.
56
+
57
+
58
+
You are now ready to take it to the next level.
41
59
42
-
With a running Elastic Stack and Serilog producing log events you are now ready to take it to the next level. If you fancy the producing part you'll dig deeper into Serilog and its configuration of log contexts, enrichers and message formatters. If you enjoy monitoring applications in production you'll explore Kibana with its visualizations and dashboards.
60
+
If you fancy the producing part you'll dig deeper into PostSharp Logging options, custom logging features, and message formatters.
61
+
If you enjoy monitoring applications in production you'll explore Kibana with its visualizations and dashboards.
43
62
44
63
## Requirements
45
64
@@ -57,7 +76,8 @@ PS> cd .\elastic-stack\
57
76
PS> docker-compose up
58
77
```
59
78
60
-
If this is the first time the stack is started, you'll have to create a Logstash index pattern. Give the stack some time to initialize and then run the following commands in PowerShell:
79
+
If this is the first time the stack is started, you'll have to create a Logstash index pattern.
80
+
Give the stack some time to initialize, then create the index manually in the UI, or run the following commands in PowerShell:
0 commit comments