You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

serilog.md 1.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ---
  2. uid: Guides.OtherLibs.Serilog
  3. title: Serilog
  4. ---
  5. # Configuring serilog
  6. ## Prerequisites
  7. - A basic working bot with a logging method as described in [Creating your first bot](xref:Guides.GettingStarted.FirstBot)
  8. ## Installing the Serilog package
  9. You can install the following packages through your IDE or go to the nuget link to grab the dotnet cli command.
  10. |Name|Link|
  11. |--|--|
  12. |`Serilog.Extensions.Logging`| [link](https://www.nuget.org/packages/Serilog.Extensions.Logging)|
  13. |`Serilog.Sinks.Console`| [link](https://www.nuget.org/packages/Serilog.Sinks.Console)|
  14. ## Configuring Serilog
  15. Serilog will be configured at the top of your async Main method, it looks like this
  16. [!code-csharp[Configuring serilog](samples/ConfiguringSerilog.cs)]
  17. ## Modifying your logging method
  18. For Serilog to log Discord events correctly, we have to map the Discord `LogSeverity` to the Serilog `LogEventLevel`. You can modify your log method to look like this.
  19. [!code-csharp[Modifying your log method](samples/ModifyLogMethod.cs)]
  20. ## Testing
  21. If you run your application now, you should see something similar to this
  22. ![Serilog output](images/serilog_output.png)
  23. ## Using your new logger in other places
  24. Now that you have set up Serilog, you can use it everywhere in your application by simply calling
  25. [!code-csharp[Log debug sample](samples/LogDebugSample.cs)]
  26. > [!NOTE]
  27. > Depending on your configured log level, the log messages may or may not show up in your console. Refer to [Serilog's github page](https://github.com/serilog/serilog/wiki/Configuration-Basics#minimum-level) for more information about log levels.