0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Asp .Net Core AzureWebServerのログをBlobストレージに送る

Posted at

loggerの設定

Program.cs
  public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    // デフォルトを上書き
                    logging.ClearProviders();
                    logging.AddAzureWebAppDiagnostics();
                    logging.AddFilter((provider, category, logLevel) =>
                    {
                        // フィルタ条件が書ける
                        return true;
                    });
                })
                .ConfigureServices(serviceCollection => serviceCollection
                    //Blobを使うよ
                    .Configure<AzureBlobLoggerOptions>(options =>
                    {
                        //出力ファイル名 (prefixにGuidが付く)
                        options.BlobName = "log.txt";
                    }))
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

App Service側の設定

スクリーンショット_102220_065012_PM.jpg

使用箇所

DIに登録されているので、コンストラクタにILogger<クラス名>でインスタンスが取得できる

public TestController(ILogger<TestController> logger)
{
    logger.LogError("エラーメッセージ");
}

Startup.csのConfigureServicesで使いたい場合は以下で取得可能

public void ConfigureServices(IServiceCollection services)
{
    var logger = services.BuildServiceProvider().GetRequiredService<ILogger<Startup>>();
}

確認

指定したBlobストレージにログが出力されるかと思います。

スクリーンショット_102420_114558_AM.jpg

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?