|
| 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