The project with project type 'worker service' cannot be logged
See original GitHub issuepublic class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddLogging(logger=>
{
logger.ClearProviders();
logger.AddNLog();
});
services.AddHostedService<Startup>();
services.AddScoped<IDbContext>(s =>
{
return new DbContext(new DbContextBuilder
{
Connection = new MySql.Data.MySqlClient.MySqlConnection(hostContext.Configuration.GetConnectionString("mysql")),
DbContextType = DbContextType.Mysql,
});
});
services.AddQuartzFactory(f =>
{
f.AddTask<GuangHuiTask>(hostContext.Configuration.GetSection("CronExpression:GuangHui").Value, typeof(TransactionTaskFilter));
});
})
.UseWindowsService(); //on windows service
}
public class Startup : BackgroundService
{
private readonly ILogger<Startup> _logger;
private readonly IQuartzFactory _factory;
public Startup(ILogger<Startup> logger, IQuartzFactory factory)
{
_factory = factory;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("test nlog logging");
await _factory.StartAsync(stoppingToken);
}
}
//application.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
//NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<target xsi:type="Console" name="c"
layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
<target xsi:type="File" name="f" fileName="${currentdir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
</targets>
<rules>
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
<logger name="*" minlevel="Trace" writeTo="f,c" />
</rules>
</nlog>
sc create YC51.GasStation.WinService binPath= C:\WindowsService\GasStationService\YC51.GasStation.WinService.exe
PAUSE
Directory list: C:\WindowsService\GasStationService\YC51.GasStation.WinService.exe C:\WindowsService\GasStationService\NLog.config …
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Serilog file logging not written from Worker Service when ...
When I configure the project to use Serilog to write to a log file, the log file is never written to (and if...
Read more >Create Windows Service using BackgroundService - .NET
To create a new Worker Service project with Visual Studio, you'd select File > New > Project.... From the Create a new project...
Read more >Running .NET Core Applications as a Windows Service
Worker Services allows for running background services through the use of a hosted service. While creating a new project in Visual Studio, we ......
Read more >Getting Started With Worker Services
To create the project: Open Visual Studio 2022; Create a new project; Type “worker” in the search bar, and choose the option “C#...
Read more >Worker Services in .NET Core 3.0 - The New Way ... - YouTube
In this video, we will look at how to create a worker service, ... Service App code design using http client 20:15 -...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Updated wiki: https://github.com/NLog/NLog/wiki/File-target#filename-directory
Remember to remove
ThrowExceptions="true"