ASP.NET Core讓Application Insight出現莫名的HTTP 401

Edward Kuo
Jul 12 · 4 min read
Image for post
Image for post

某一天看到Application Insights所監控的資料,發現失敗的要求量怎樣多,實在是不太可能有這樣子,且每個發生頻率時間又有一點規則

Image for post
Image for post

因此,覺得這因該有那邊不正常,因此去搜尋一下紀錄,確實看到有很多的HTTP 401的資訊,所以,造成失敗的要求技數增加也是正常。

Image for post
Image for post

不過,發生這個錯誤的401是那邊來的,記得其他系統都是相同設置,也沒有發生這問題。如果今日把Azure App Server的Always On 關掉,就一陣子就沒這訊息,但這不是正常,因為網站都沒有活動當然不會有這樣錯誤的Request,不過,從這邊至少可以得到的資訊是這樣錯誤不太像是外部所引發。

因此,開始從內部相關設定去找。原本以為想說會不會是Application Insights的智慧偵測設定所造成的

Image for post
Image for post

也嘗試把這些都關掉,但是依舊還是有發生這錯誤資訊。不過,從Application Insights 多個Log的蛛絲馬跡可以看得到,因該是發生了Request Starting 然後就造成401現象發生

Image for post
Image for post

那樣這個的Request Starting是從那邊來的?因此,找尋Request Starting的Request的詳細資訊,從這個Request的Custom Properties內看到一個熟悉的字眼-Microsoft.AspNetCore.Hosting.Diagnostics

Image for post
Image for post

而會有這個事件發生,主要在於ASP.NET Core 中的健康狀態檢查 的功能,在ASP.NET Core中如果有搭配到其他監視服務,就會Enable這樣機制。剛好又搭配Application Insights,因此就被啟動。但是為什麼其他Azure App Server也是這樣做,就不會發生這個訊息產生,主要在於一旦啟動這監控服務,應用程式會將健康狀態檢查公開為 HTTP 端點,而這個App Service是有被限定存取範圍限制,要某個IP範圍內才可以存取此App Service,也因此造成上述所謂的公開的HTTP端點被阻斷了,造成機制啟動後,一直就會發生401錯誤。

所以,要避免這個問題產生,作法就是必須關掉健康狀態檢查 的機制。因此,必須在appsettings.json加入下面這段

Logging”: {
“LogLevel”: {
“Default”: “Warning”,
“Microsoft”: “Warning”,
“Microsoft.Hosting.Lifetime”: “Information”, “Microsoft.AspNetCore.Hosting.Diagnostics”: “None”
}
}

最主要Microsoft.AspNetCore.Hosting.Diagnostics要把它設定為None,就可以停止.NET Core 自我的健康檢查機制,進而避免發生這個錯誤資訊

參考資料

  1. https://docs.microsoft.com/zh-tw/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-3.1

EK.Technology Learn

Design,Thinking,Coding & have fun every thing

Edward Kuo

Written by

Enterprise IT Manager / Microsoft Regional Director / Microsoft MVP / DevOps Expert / Speaker, About me: https://profile.edwardkuo.dev/about/

EK.Technology Learn

Design,Thinking,Coding & have fun every thing

Edward Kuo

Written by

Enterprise IT Manager / Microsoft Regional Director / Microsoft MVP / DevOps Expert / Speaker, About me: https://profile.edwardkuo.dev/about/

EK.Technology Learn

Design,Thinking,Coding & have fun every thing

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store