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.

logging.md 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ---
  2. title: Logging
  3. ---
  4. Discord.Net's clients provide a [Log] event that all messages will be
  5. disbatched over.
  6. For more information about events in Discord.Net, see the [Events]
  7. section.
  8. [Log]: xref:Discord.Rest.BaseDiscordClient#Discord_Rest_BaseDiscordClient_Log
  9. [Events]: events.md
  10. ### Usage
  11. To receive log events, simply hook the discord client's log method
  12. to a `Task` with a single parameter of type [LogMessage].
  13. It is recommended that you use an established function instead of a
  14. lambda for handling logs, because most addons accept a reference
  15. to a logging function to write their own messages.
  16. [LogMessage]: xref:Discord.LogMessage
  17. ### Usage in Commands
  18. Discord.Net's [CommandService] also provides a log event, identical
  19. in signature to other log events.
  20. Data logged through this event is typically coupled with a
  21. [CommandException], where information about the command's context
  22. and error can be found and handled.
  23. [CommandService]: xref:Discord.Commands.CommandService
  24. [CommandException]: xref:Discord.Commands.CommandException
  25. #### Samples
  26. [!code-csharp[Logging Sample](samples/logging.cs)]
  27. #### Tips
  28. Due to the nature of Discord.Net's event system, all log event
  29. handlers will be executed synchronously on the gateway thread. If your
  30. log output will be dumped to a Web API (e.g. Sentry), you are advised
  31. to wrap your output in a `Task.Run` so the gateway thread does not
  32. become blocked while waiting for logging data to be written.