[英]azure function not logging exceptions to application insight
我有具有服務總線主題觸發器的天藍色功能。 我注意到,當函數毫無例外地執行時,所有 tracewriter.loginformation 消息都會正確顯示在應用程序洞察力中。 但如果發生任何異常,則在 appinsights 中顯示夜間 log.information 也不顯示 log.error
我的功能在消費計划下運行。 我注意到我有 2 個 catch 塊。
catch(validation ex)
{
log.error;
}
catch(exception ex)
{
log.error;
throw;
}
第一個 catch 塊中的所有錯誤都被記錄,但第二個 catch 塊中的所有錯誤都沒有被記錄。 我注釋掉了 throw 並且它開始記錄到 appinsights .. 但我需要 throw 進行死信處理。 請指教。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<Version>1.6.15</Version>
<DependsOnNETStandard>netstandard1.5</DependsOnNETStandard>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="2.1.0-beta1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="1.0.0-beta1" />
<PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.1.0-beta1" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.0-alpha6" />
<PackageReference Include="System.IO" Version="4.1.0" />
<PackageReference Include="System.Runtime" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Update="TemplateResource.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>TemplateResource.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="TemplateResource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TemplateResource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\PublishProfiles\" />
</ItemGroup>
</Project>
我在該log.Error
中遇到了類似的問題。錯誤(“我的錯誤消息”)總是導致 Application Insights 中出現跟蹤而不是異常(這是我想要的)。 檢查 Application Insights SDK 的源代碼后,很明顯要在 Application Insights 中獲取異常,您必須將異常對象傳遞給LogError調用。
例子:
log.Error(ex, "my error message") - will result in Application Insight Exception
log.Error("my error message") - will result in Application Insight Trace
我發現當使用null
或string.Empty
將消息或異常記錄到 Application Insights 時,該異常不會保存在 Application Insights 中,即使它顯示在 Visual Studio 的本地 Application Insights 預覽窗口中也是如此。
為了減輕這種情況,我使用string.IsNullOrWhiteSpace
檢查了消息並將其替換為例如字符串"No message"
。
在你的情況下,這意味着這樣的事情:
// Preventing an empty message from being logged.
var message = string.IsNullOrWhiteSpace(ex.Message) ? "No message" : ex.Message;
log.error(ex, message);
我試圖用你的版本重現它,它仍然對我有用。
try
{
if (new Random().NextDouble() > 0.5)
{
throw new ArgumentNullException("null");
}
throw new Exception("some exception");
}
catch (ArgumentNullException)
{
log.Error("null");
}
catch (Exception e)
{
log.Error("some exception");
throw;
}
我在 Application Insights 中同時看到:“null”和“一些異常”。
順便說一句,已經有穩定的 Microsoft.NET.Sdk.Functions 1.0.14,它應該包括自 1.0.0-alpha6 以來的許多修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.