How to enrich Serilog’s custom dimensions for application insights

I have tried a few different methods to log some custom properties to app insights using Serilog, but none of them show up in the logs. First I tried using the Enrich.FromLogContext() method:

Program.cs:

public static IHostBuilder CreateHostBuilder() =>

            new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureAppConfiguration((_, _) =>
                {
                })
                .UseSerilog((context, _, configuration) =>
                {
                    configuration.MinimumLevel.Override("System", LogEventLevel.Information);
                    configuration.MinimumLevel.Override("Microsoft", LogEventLevel.Information);
                    configuration.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning); // Suppressing AspNetCore framework log noise
                    configuration.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning); // Suppressing EntityFrameworkCore framework log noise
                    configuration.ReadFrom.Configuration(context.Configuration);
                    configuration.WriteTo.Console();
                    configuration.Enrich.FromLogContext();
                })
                .ConfigureServices((context, services) =>
                {
                    var configuration = context.Configuration;
                    services.AddApplicationInsightsTelemetry(configuration);
                    services.AddLogging(logBuilder => logBuilder.AddSerilog(dispose: true));
                 }

And then adding a custom property to the log context in my class:

MyClass.cs:

private ILogger Log => Serilog.Log.ForContext(GetType());

    public async Task Run()
    {
        using (LogContext.PushProperty("FunctionName", "MyClass.Run"))
        {
            Log.Here().Information("Logging from MyClass.Run()");
        }
     }

The property “FunctionName” did not show up along with the log message in application insights.

Next I tried explicitly adding a property to a single log message:

//logic here
Log.Here().Information("Logging from function {FunctionName}, "Run");
//logic here

But that didn’t show up either. Just to clarify: the messages are being logged, but the custom properties are not included in the customDimensions field in application insights. Am I configuring something wrong?

EDIT: I found a solution, but not quite sure why it works. If I add the following to my Serilog configuration:

configuration.WriteTo.ApplicationInsights(context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"], TelemetryConverter.Traces);

the custom dimensions are added to the logs. However, every line is also logged twice, presumably because it logs once with the WriteTo method and once with the built in logging from services.AddApplicationInsightsTelemetry(configuration);. What I don’t understand is why logs from AddApplicationInsightsTelemetry doesn’t include the custom dimensions.

2

I don’t understand is why logs from AddApplicationInsightsTelemetry doesn’t include the custom dimensions.

To log custom dimensions using default ApplicationInsightsSDK, you need to use telemetry client.

  • By default, it gives a basic custom property.
  • To log any additional dimensions, you need to use Telemetry Client, which you have done later.

You can also refer my SOThread to log additional custom dimensions.

This MSDoc also explains the same

When you use the standalone package, TelemetryClient isn’t injected to the dependency injection . You need to create a new instance of TelemetryClient and use the same configuration that the logger provider uses.

As per this GitHub, you can use TelemetryClient or create your own ITelemetryConverter to log additional custom properties.

Custom Dimensions with TelemetryClient using AddApplicationInsightsTelemetry

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật