Hi @Cenk,
Whether the exe
file you use to create the Windows service is the original exe
file or the published file. I built a simple example, using the exe
after publishing to create a Windows service, the path of the output log file is changed.
NuGet Packages used: Microsoft.ApplicationInsights.AspNetCore, Microsoft.ApplicationInsights.WorkerService, Microsoft.Extensions.Hosting, Microsoft.Extensions.Hosting.WindowsServices, Serilog.AspNetCore, Serilog.Extensions.Hosting, Serilog.Sinks.Map.
Worker.cs:
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly TelemetryClient _telemetryClient;
public Worker(ILogger<Worker> logger,
TelemetryClient telemetryClient,
IConfiguration configuration)
{
_logger = logger;
_telemetryClient = telemetryClient;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
using (_telemetryClient.StartOperation<RequestTelemetry>("Execute Async"))
{
Log.Information("TestLog-Serilog");
_logger.LogInformation("TestLog-Logger");
}
await Task.Delay(1000, stoppingToken);
}
}
}
Program.cs:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.File("./logs/log-.txt", rollingInterval: RollingInterval.Day)
.CreateBootstrapLogger();
try
{
Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "Razer Bulk Service";
})
.ConfigureAppConfiguration((hostContext, configBuilder) =>
{
configBuilder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.MachineName}.json", true, true)
.Build();
})
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext())
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddApplicationInsightsTelemetryWorkerService();
})
.Build();
await host.RunAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "There was a problem starting the service");
}
finally
{
Log.Information("Service successfully stopped");
Log.CloseAndFlush();
}
appsetting.json:
{
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
},
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ApplicationInsights": {
"InstrumentationKey": "the-appinsights-guid"
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Debug" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": ".\\logs\\log-.txt",
"rollingInterval": "Day"
}
}
],
"Enrich": [ "FromLogContext" ]
}
}
launchSettings.json:
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Production"
}
Then publish it and use the published exe
file to create a Windows service:
sc.exe create "Razer Bulk Service" binpath="Your Project Path\bin\Release\net6.0\publish\2023041101.exe"
When I run the project, the log file is generated in \bin\Debug\net6.0\logs
and successfully outputs the log content:
When I run the Windows service:
sc.exe start "Razer Bulk Service"
The log file is generated in \bin\Release\net6.0\publish\logs
and successfully outputs the log content:
You can check it.
If the answer is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Chen Li