0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Funtions でログレベルを Warning 以上にして Information ログが出力されないようにしてみた

Posted at

Azure Functions のログ出力で「必要なログだけ」出力させたい場合、どうするのが良いのだろう。ローカルで開発しているときは、それなりの情報量のログ出力を確認したいが、Azure 環境下では必要なログだけにしたい。例えば、条件付きコンパイルで #if Debug を使ってログ出力を出し分けるとか。条件付きコンパイルがコードの色んなところにあると単純にコードが長くなってしまいます。そこで今回は、Azure 環境下でも出したいログは、Warning レベルにして Information ログが出力されないか試してみました。

Azure Functions 検証環境を作成

bash
prefix=mnrfnlog
region=japaneast

az group create \
  --name ${prefix}-rg \
  --location $region

az storage account create \
  --name ${prefix}stor \
  --resource-group ${prefix}-rg \
  --sku Standard_LRS

az functionapp create \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --consumption-plan-location $region \
  --runtime dotnet \
  --runtime-version 6 \
  --functions-version 4 \
  --storage-account ${prefix}stor \
  --https-only \
  --os-type Linux \
  --assign-identity

Azure Functions 検証アプリを作成

bash
func init $prefix --dotnet

cd $prefix

func new --name Example --template HttpTrigger --authlevel anonymous

func start

curl http://localhost:7071/api/Example

ログ出力を確認

[2024-10-05T23:40:58.041Z] Executing 'Example' (Reason='This function was programmatically called via the host APIs.', Id=288594af-f60e-434c-9c56-bf5de5ed7ce6)
[2024-10-05T23:40:58.050Z] C# HTTP trigger function processed a request.
[2024-10-05T23:40:58.081Z] Executed 'Example' (Succeeded, Id=288594af-f60e-434c-9c56-bf5de5ed7ce6, Duration=47ms)

Azure Functions にデプロイしてログ出力を確認

bash
func azure functionapp publish ${prefix}

curl https://$prefix.azurewebsites.net/api/example

functions-logleve-01.png

アプリに Warning レベルのログを追加

適当なところで name 変数の出力を確認するコードを追加

Example.cs
log.LogWarning($"name={name}");

アプリの動作確認

bash
func start

curl "http://localhost:7071/api/Example?name=test"

name=test が出力されていることを確認

[2024-10-05T23:49:00.294Z] Executing 'Example' (Reason='This function was programmatically called via the host APIs.', Id=8cb3d753-1a87-4563-87be-6a5978f160d7)
[2024-10-05T23:49:00.302Z] C# HTTP trigger function processed a request.
[2024-10-05T23:49:00.321Z] name=test
[2024-10-05T23:49:00.331Z] Executed 'Example' (Succeeded, Id=8cb3d753-1a87-4563-87be-6a5978f160d7, Duration=44ms)

Azure Functions にデプロイして動作確認

bash
func azure functionapp publish ${prefix}

curl "https://$prefix.azurewebsites.net/api/example?name=Azure"

種類が Warning で name=Azure を確認

functions-logleve-02.png

環境変数でログレベルを Warning 以上に設定

bash
az functionapp config appsettings set \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --settings "AzureFunctionsJobHost__logging__logLevel__Function=Warning"

functions-logleve-03.png

Warning レベルだけログ出力されることを確認

bash
curl "https://$prefix.azurewebsites.net/api/example?name=Warning"

functions-logleve-04.png

参考

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?