I have create a worker using "dotnet new worker" but using code bellow no events are written to EventLog:
program.cs
````csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;
namespace CoreWindowsService
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceBaseLifetime()
.ConfigureLogging(loggerFactory => loggerFactory
.AddEventLog(new EventLogSettings() {SourceName = "CoreWindowsService"}))
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
});
}
}
````
worker.cs
````csharp
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace CoreWindowsService
{
public class Worker : BackgroundService
{
private readonly ILogger
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
public override async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Iniciado: {DateTime.Now}");
await Task.CompletedTask;
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Parado: {DateTime.Now}");
await Task.CompletedTask;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation($"Executando: {DateTime.Now}");
await Task.Delay(1000, stoppingToken);
}
}
}
}
````
Attaching debug session to installed service, nothing happen!
I have discover that problem is StartAsync() and StopAsync() methods. This happen because I used override on methods, change to:
````csharp
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Iniciado: {DateTime.Now}");
await Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Parado: {DateTime.Now}");
await Task.CompletedTask;
}
````
Works fine!
But I receive an alert about "override"!
Yes this is because you need to call the base in StartAsync and StopAsync (which I agree is a bit unclear). Changing this wild be a breaking change at this point so I鈥檓 not sure how much we鈥檇 be willing to do here
@davidfowl Maybe you can change sample at blog post including Start and Stop calls!
@davidfowl Now it's working:
````csharp
public override async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Iniciado: {DateTime.Now}");
await base.StartAsync(cancellationToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Parado: {DateTime.Now}");
await base.StopAsync(cancellationToken);
}
````
@carloscds - should this issue be closed? Or do you have any further questions?
Do you mean my blog posts about running as a Windows Service? I am happy with that.
@Eilon I think we add the doc label to this to make sure we have a note in any official docs about this scenario. I'll call it out in an up-coming blog post to help anyone else who gets caught by it.
@Eilon Yes, you can close.
@glennc Yes, I read you blog post. Very usefull, maybe you can add Start/Stop in you sample!
Oh I guess we'll keep open because this is a doc issue now.
@anurse can you assign?
Sure, I assign it to myself :). https://github.com/aspnet/AspNetCore.Docs/pull/14642
@anurse looks like the docs article is done. Should this be closed?