Reputation: 23
I am using the serilog application insights sink in a .NET project and it is showing requests but not traces in the Azure portal.
The application insights telemetry window in VS shows local app insights which is showing the requests and traces but in the azure portal I can only see requests. I have tried to use the minimal configuration to get it working but I just cant see traces at all. I have a file sink which shows the log messages and the requests. What am I missing?
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
builder.Host.UseSerilog(logger);
builder.Services.AddApplicationInsightsTelemetry();
_logger.LogInformation("Test Log Entry");
The config file I am using:
{
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=XXXX",
},
"AllowedHosts": "*",
"Serilog": {
"Using": [
"Serilog.Sinks.ApplicationInsights"
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "O:\\logs\\log1.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"fileSizeLimitBytes": 20000000,
"rollOnFileSizeLimit": true
}
},
{
"Name": "ApplicationInsights",
"Args": {
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
],
"Enrich": [ "FromLogContext" ]
}
}
Upvotes: 1
Views: 1442
Reputation: 7347
Initially even I got the traces only in Local, unable to send traces to Application Insights.
I have used the same configuration which you have shared, done changes in the Program.cs
file.
To log requests/Traces using Serilog
, we need to use the LoggerConfiguration
.
Install the below NuGet Packages
Serilog
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights
My .csproj
file:
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
My Program.cs
file:
using Serilog;
using Serilog.Sinks.ApplicationInsights.TelemetryConverters;
var builder = WebApplication.CreateBuilder(args);
var Conn = builder.Configuration.GetSection("ApplicationInsights").GetValue<string>("ConnectionString");
var log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.ApplicationInsights(Conn, new TraceTelemetryConverter())
.CreateLogger();
builder.Logging.AddSerilog(log);
builder.Services.AddRazorPages();
var app = builder.Build();
app.Logger.LogInformation("Log Information from Program class file");
app.Logger.LogWarning("Log warning from Program.cs");
---
}
Traces in Application Insights:
Upvotes: 1