LoginSignup
0
0

WPFでDIからSerilogでのログ出力をやってみた

Posted at

環境

  • 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を使用するだけで使用可能。
これだけでログ出力の設定が出来るんだから、便利な世の中になったもんだ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0