Code: Select all
public sealed class WindowsBackgroundService (
MyService _myService,
ILogger _logger)
: BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await _my.OnStart(stoppingToken);
while (!stoppingToken.IsCancellationRequested)
await _my.DoWorkAsync(stoppingToken).ConfigureAwait(false);
}
< /code>
usw. Ich registriere den Dienst mit sc.exe create
In AppStings.json Ich habe die folgende Konfiguration:
Code: Select all
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": {
"SourceName": "My Service Name",
"LogName": "Application",
"LogLevel": {
"Default": "Warning",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
Code: Select all
internal static partial class LogMessage
{
[LoggerMessage(Level = LogLevel.Warning, Message = "Bar")]
public static partial void Bar(ILogger logger);
< /code>
mit Aufruf: < /p>
LogMessage.Bar(_logger);
[*] Registrieren Sie diese IDs irgendwie mit dem Ereignisprotokoll, damit der Warntext nicht angezeigt wird, oder < /li>
Loggermessage senden alle Nachrichten senden mit id 0 beim Sprechen mit dem Ereignisprotokoll.
[/u]