Reputation: 627
I have azure function that has service bus topic trigger. I am noting that when function executes without any exceptions then all tracewriter.loginformation message appear properly in application insights. but if any exception occurs then nighther log.information is shown nor log.error is shown in appinsights
My function running under consumption plan. I noted that i have 2 catch blocks.
catch(validation ex)
{
log.error;
}
catch(exception ex)
{
log.error;
throw;
}
all errors in first catch block getting logged but all errors in second catch block not getting logged. i commented out throw and it started logging to appinsights.. but i need throw for deadlettering. please advise.
<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>
Upvotes: 1
Views: 3747
Reputation: 1352
I've found that when logging a message or excpeption to Application Insights with null
or string.Empty
the exception is not saved in Application Insights even though it shows up in the local Application Insights preview window in Visual Studio.
To mitigate that I've checked the message with string.IsNullOrWhiteSpace
and replaced it with e.g. the string "No message"
.
In your case that would mean something like this:
// Preventing an empty message from being logged.
var message = string.IsNullOrWhiteSpace(ex.Message) ? "No message" : ex.Message;
log.error(ex, message);
Upvotes: 1
Reputation: 1393
I had a similar problem in that log.Error
("my error message") was always resulting in a Trace in Application Insights instead of an Exception (which is what I wanted). After inspecting the source code of the Application Insights SDK it became apparent that to get an Exception in Application Insights you must pass an exception object into the LogError call.
Example:
log.Error(ex, "my error message") - will result in Application Insight Exception
log.Error("my error message") - will result in Application Insight Trace
Upvotes: 7
Reputation: 351
I have tried to repro it with your versions and it still works for me.
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;
}
I see both: 'null' and 'some exception' in Application Insights.
BTW, there is already stable Microsoft.NET.Sdk.Functions 1.0.14, it should include many fixes since 1.0.0-alpha6.
Upvotes: 0