1. 创建控制台项目
添加 nuget 包依赖
点击查看代码
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0"/>
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.3" />
</ItemGroup>
2. 修改 Program.cs 文件
点击查看代码
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
namespace Lesson01;
internal class Program
{
private static async Task Main(string[] args)
{
await CreateHostBuilder(args).Build().RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureHostOptions(options =>
{
options.ShutdownTimeout = TimeSpan.FromSeconds(30);
options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
})
.ConfigureHostConfiguration(configure =>
{
//configure.AddJsonFile("appsettings.json", true);
})
.ConfigureServices((services) =>
{
// add logger service
services.AddLogging(configure =>
{
configure.ClearProviders();
configure.AddNLog("nlog.config");
});
});
}
}
3. 添加通用主机
4. 添加日志及配置文件
点击查看代码
// add logger service
services.AddLogging(configure =>
{
configure.ClearProviders();
configure.AddNLog("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"
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
-->
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!-- info log -->
<target xsi:type="File"
name="Information"
encoding="UTF-8"
layout="${longdate:format=yyyy-MM-ddTHH:mm:ss.fffZ} |${level:uppercase=true:padding=-5}| ${message} ${exception}"
fileName="${basedir}/logs/message.log"
archiveFileName="${basedir}/logs/message.{####}.log"
archiveNumbering="Rolling"
archiveAboveSize="10485760"
concurrentWrites="true"
maxArchiveFiles="20"
keepFileOpen="true" />
<target xsi:type="File"
name="microsoft"
encoding="UTF-8"
layout="${longdate:format=yyyy-MM-ddTHH:mm:ss.fffZ} |${level:uppercase=true:padding=-5}| ${logger} | ${message} ${exception}"
fileName="${basedir}/logs/microsoft.log"
archiveFileName="${basedir}/logs/microsoft.{####}.log"
archiveNumbering="Rolling"
archiveAboveSize="10485760"
concurrentWrites="true"
maxArchiveFiles="20"
keepFileOpen="true" />
</targets>
<!-- network log -->
<targets>
<target name="Net_Info" xsi:type="Network" address="tcp://localhost:4001"/>
<target name="Net_Error" xsi:type="Network" address="tcp://localhost:4002"/>
<target name="Net_Warn" xsi:type="Network" address="tcp://localhost:4003"/>
</targets>
<!-- console log -->
<targets>
<!-- write log message to console
EventId_Id=1001, EventId_Name=TestHostedService, EventId=TestHostedService| TestHostedService-->
<target xsi:type="ColoredConsole" name="simplify" useDefaultRowHighlightingRules="false"
layout="[${longdate}] |${level}| ${message}">
<highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" />
<highlight-row condition="level == LogLevel.Info" foregroundColor="Green" />
<highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
<highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
<highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White"/>
</target>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="Lesson01.*" minlevel="Debug" writeTo="Information" />
<logger name="Lesson01.*" minlevel="Debug" writeTo="simplify" />
<logger name="Microsoft.Extensions.*" minlevel="Debug" writeTo="Information" />
<logger name="Microsoft.Hosting.*" minlevel="Trace" writeTo="simplify" />
</rules>
</nlog>
点击查看代码
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Lesson01;
public class GreetingService : BackgroundService
{
private readonly ILogger<GreetingService> m_Logger;
private readonly IHostApplicationLifetime m_ApplicationLifetime;
public GreetingService(ILogger<GreetingService> logger,
IHostApplicationLifetime applicationLifetime)
{
m_Logger = logger;
m_ApplicationLifetime = applicationLifetime;
m_ApplicationLifetime.ApplicationStarted.Register(async () => { await OnStarted().ConfigureAwait(false); });
m_ApplicationLifetime.ApplicationStopping.Register(OnStopping);
m_ApplicationLifetime.ApplicationStopped.Register(OnStopped);
}
public override async Task StartAsync(CancellationToken token)
{
await base.StartAsync(token);
}
public override async Task StopAsync(CancellationToken token)
{
await base.StopAsync(token);
}
protected override async Task ExecuteAsync(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
m_Logger.LogWarning("Hello World!");
await Task.Delay(TimeSpan.FromSeconds(3), token);
}
}
#region Override Methods
private async Task OnStarted()
{
m_Logger.LogInformation("OnStarted has been called.");
await Task.CompletedTask;
}
private void OnStopping()
{
m_Logger.LogInformation("OnStopping has been called.");
}
private void OnStopped()
{
m_Logger.LogInformation("OnStopped has been called.");
}
#endregion
}
将 GreetingService 添加到容器中
点击查看代码
// add hosted server
services.AddHostedService<GreetingService>();