環境
- dotnet6
- C#
Nugetパッケージ
- Serilog.AspNetCore
- CommunityToolkit.Mvvm
内容
- Program.cs
namespace SerilogTeest
{
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
// Serilogの設定
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Debug()
.WriteTo.File(
@"C:\Logs\SerilogTeest\.txt",
rollingInterval: RollingInterval.Day)
.CreateLogger();
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(loggingBuilder =>
loggingBuilder.AddSerilog(dispose: true));
Ioc.Default.ConfigureServices(serviceProvider);
}
}
}
設定説明
@"C:\Logs\SerilogTeest\.txt",
出力するログのパス。RollingInterval.Dayを設定してあるので、yyyyMMdd.txtの形で出力される。
rollingInterval: RollingInterval.Day
この設定で、C:\Logs\SerilogTeestにyyyyMMdd日付のログが出力される。
感想
あとはILoggerを使用するだけで使用可能。
これだけでログ出力の設定が出来るんだから、便利な世の中になったもんだ。