Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 9ba5c08

Browse files
committed
Raggruppata documentazione SERILOG
1 parent cc4f4fb commit 9ba5c08

File tree

2 files changed

+241
-0
lines changed

2 files changed

+241
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Serilog configuration
2+
3+
4+
## Configuration to add to the appsettings.json file
5+
6+
```json
7+
"Serilog": {
8+
"MinimumLevel": "Debug",
9+
"WriteTo": [
10+
{
11+
"Name": "Console",
12+
"Args": {
13+
"outputTemplate": "{Timestamp:HH:mm:ss}\t{Level:u3}\t{SourceContext}\t{Message}{NewLine}{Exception}"
14+
}
15+
},
16+
{
17+
"Name": "File",
18+
"Args": {
19+
"path": "Logs/log.txt",
20+
"rollingInterval": "Day",
21+
"retainedFileCountLimit": 14,
22+
"restrictedToMinimumLevel": "Warning",
23+
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
24+
}
25+
}
26+
]
27+
}
28+
```
29+
30+
31+
## Registering services at Startup
32+
33+
```csharp
34+
public Startup(IConfiguration configuration)
35+
{
36+
Configuration = configuration;
37+
}
38+
39+
public IConfiguration Configuration { get; }
40+
41+
public void ConfigureServices(IServiceCollection services)
42+
{
43+
services.AddSerilogServices();
44+
}
45+
46+
//OMISSIS
47+
48+
public void Configure(WebApplication app)
49+
{
50+
app.AddSerilogConfigureServices();
51+
}
52+
```
53+
54+
55+
## Registering services at Program
56+
57+
```csharp
58+
public static void Main(string[] args)
59+
{
60+
var builder = WebApplication.CreateBuilder(args)
61+
.AddSerilogOptionsBuilder();
62+
63+
Startup startup = new(builder.Configuration);
64+
65+
//OMISSIS
66+
}
67+
```
68+
69+
70+
## Example of use in a web api controller
71+
72+
```csharp
73+
[ApiController]
74+
[Route("api/[controller]")]
75+
public class PeopleController : ControllerBase
76+
{
77+
private readonly ILoggerService logger; //required using NET6CustomLibrary.Serilog.Services;
78+
79+
public PeopleController(ILoggerService logger)
80+
{
81+
this.logger = logger;
82+
}
83+
84+
[HttpGet("people")]
85+
public async Task<IActionResult> GetPeople()
86+
{
87+
//OMISSIS
88+
89+
logger.SaveLogInformation("YOUR INFORMATION MESSAGE");
90+
91+
//OMISSIS
92+
}
93+
94+
//OMISSIS
95+
}
96+
```
97+
98+
99+
## Types of loggers provided in the ILoggerService class
100+
101+
- CRITICAL
102+
- ERROR
103+
- WARNING
104+
- INFORMATION
105+
- DEBUG
106+
107+
<b>Note:</b> Required using NET6CustomLibrary.Serilog.Services of the NET6CustomLibrary Nuget package
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Serilog configuration with SEQ platform
2+
3+
4+
## Configuration to add to the appsettings.json file (Writing logs to console, to txt file and to SEQ platform)
5+
6+
<b>Note:</b> If the logs must be saved only on SEQ, comment out and/or delete the part of the configuration linked to the FILE and CONSOLE sections.
7+
8+
The minimum level of traced logs is DEBUG.
9+
10+
```json
11+
"Serilog": {
12+
"MinimumLevel": "Debug",
13+
"WriteTo": [
14+
{
15+
"Name": "Console",
16+
"Args": {
17+
"outputTemplate": "{Timestamp:HH:mm:ss}\t{Level:u3}\t{SourceContext}\t{Message}{NewLine}{Exception}"
18+
}
19+
},
20+
{
21+
"Name": "File",
22+
"Args": {
23+
"path": "Logs/log.txt",
24+
"rollingInterval": "Day",
25+
"retainedFileCountLimit": 14,
26+
"restrictedToMinimumLevel": "Information",
27+
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
28+
"outputTemplate": "{Timestamp:HH:mm:ss}\t{Level:u3}\t{SourceContext}\t{Message}{NewLine}{Exception}"
29+
}
30+
},
31+
{
32+
"Name": "Seq",
33+
"Application": "Sample API",
34+
"Args": {
35+
"serverUrl": "http://server-seq:5341",
36+
"ApiKey": "YOUR-APIKEY",
37+
"restrictedToMinimumLevel": "Information",
38+
"outputTemplate": "{Timestamp:HH:mm:ss}\t{Level:u3}\t{SourceContext}\t{Message}{NewLine}{Exception}"
39+
}
40+
}
41+
]
42+
}
43+
```
44+
45+
46+
## Registering services at Startup
47+
48+
```csharp
49+
public Startup(IConfiguration configuration)
50+
{
51+
Configuration = configuration;
52+
}
53+
54+
public IConfiguration Configuration { get; }
55+
56+
public void ConfigureServices(IServiceCollection services)
57+
{
58+
services.AddSerilogSeqServices();
59+
}
60+
61+
//OMISSIS
62+
63+
public void Configure(WebApplication app)
64+
{
65+
//OMISSIS
66+
67+
app.AddSerilogConfigureServices();
68+
69+
//OMISSIS
70+
}
71+
```
72+
73+
74+
## Registering services at Program
75+
76+
```csharp
77+
public static void Main(string[] args)
78+
{
79+
var builder = WebApplication.CreateBuilder(args)
80+
.AddSerilogOptionsBuilder();
81+
82+
Startup startup = new(builder.Configuration);
83+
84+
//OMISSIS
85+
}
86+
```
87+
88+
89+
## Example of use in a web api controller
90+
91+
```csharp
92+
[ApiController]
93+
[Route("api/[controller]")]
94+
public class PeopleController : ControllerBase
95+
{
96+
private readonly ILoggerService logger; //required using NET6CustomLibrary.Serilog.Services;
97+
98+
public PeopleController(ILoggerService logger)
99+
{
100+
this.logger = logger;
101+
}
102+
103+
[HttpGet("people")]
104+
public async Task<IActionResult> GetPeople()
105+
{
106+
//OMISSIS
107+
108+
logger.SaveLogInformation("YOUR INFORMATION MESSAGE");
109+
110+
//OMISSIS
111+
}
112+
113+
//OMISSIS
114+
}
115+
```
116+
117+
118+
## Docker SEQ
119+
120+
An example of a Docker configuration of SEQ can be found [here](https://github.com/AngeloDotNet/Docker.Application/tree/master/Seq).
121+
122+
For SEQ (docker version) the first boot occurs without any form of active login.
123+
After the docker is active, navigate to the SETTINGS > USERS section to enable authentication to access the dashboard.
124+
125+
126+
## Types of loggers provided in the ILoggerService class
127+
128+
- CRITICAL
129+
- ERROR
130+
- WARNING
131+
- INFORMATION
132+
- DEBUG
133+
134+
<b>Note:</b> Required using NET6CustomLibrary.Serilog.Services of the NET6CustomLibrary Nuget package

0 commit comments

Comments
 (0)