.net core 應用程式 在AppService進行dotnet dump
當應用程式有High Memory 或是High Cpu的現象時,若希望對應用程式進行Runtime的分析,以前在IIS上常常進行的是在工作管理員對w3wp進行傾印,移到雲端又是Container形式後,跟以往 在windows的經驗不大一樣,不得其門而入,以下記錄一下作法
在Container For AppService的環境下要能進行Dump,會有以下 步驟
Container安裝dotnet-dump 的tool
# 前略...
# Open port 2222 for SSH access
EXPOSE 80 2222
# dotnet
RUN apk add icu-libs tzdata
RUN apk --update add libgdiplus ttf-dejavu
RUN apk add --no-cache bash curl icu-libs libc6-compat \
&& curl -sSL https://dot.net/v1/dotnet-install.sh -o /tmp/dotnet-install.sh \
&& bash /tmp/dotnet-install.sh --channel 8.0 --install-dir /usr/share/dotnet
# 安裝 dotnet-dump
RUN /usr/share/dotnet/dotnet tool install -g dotnet-dump \
&& ln -s /root/.dotnet/tools/dotnet-dump /usr/bin/dotnet-dump \
&& dotnet --info
# 後略...
確認Docker Build成功

通常你的webapp有多個 instance,請先設定環境變數增加以下 設定為true
WEBSITES_ENABLE_APP_SERVICE_STORAGE
這個設定是讓 多個instance的/home目錄共用,後續可以透過kudu的newui進行FileManager下載Dump檔案
透過進階工具連到開發工具(不是上面的SSH,這個外層的SSH應該主要是連到Container Host的主機,要連到指定的Instance的話,可以進到Kudu去指定Instance)


接著ssh連進去 確認Container內dotnet-dump版本
/home/LogFiles# dotnet-dump --version
9.0.553101+5b61d34de04d6100e6003415f7d7e9c4b971afd4
確認指定ok後,查詢process id
ps aux | grep dotnet
16 root 5h23 dotnet /app/example.dll
10891 root 0:00 grep dotnet
進行dump collect,注意,此步驟在linux container進行時,並不會中斷process或重啟(這跟我們之前w3wp.exe的經驗不太一樣,不過或許還是會間接造成伺服器比較忙錄,所以可以離峰進行)
dotnet-dump collect -p [process-id] --type Full --diag
Writing full to /home/LogFiles/core_20250211_010126
Complete
成功後
透過kudu 的FileManager就可以找到檔案 進行下載分析~~

Reference :