最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Centralized logging support with OpenTelemetry and Serilog - Stack Overflow

programmeradmin4浏览0评论

I am currently using a centralized configuration for OpenTelemetry logging. This configuration is then called at startup from every single service directly in Program.cs

With this configuration I am able to send every log to an otel collector instance that I have configured.

private static IServiceCollection AddTelemetryLogging(this IServiceCollection services, ISystemConfiguration systemConfiguration) {
    IOpenTelemetryObservabilityConfiguration otelConfiguration = systemConfiguration.GetOtelConfiguration();

    services.AddLogging(loggingBuilder => {
        var resourceBuilder = ResourceBuilder
            .CreateDefault()
            .AddService(otelConfiguration.ServiceName);
        loggingBuilder.ClearProviders();
        loggingBuilder.AddConsole();
        loggingBuilder.AddDebug();
        loggingBuilder
            .AddOpenTelemetry(options => {
                options
                    .SetResourceBuilder(resourceBuilder)
                    .AddOtlpExporter(options => {
                        options.Endpoint = new Uri(otelConfiguration.CollectorUri);
                        options.ExportProcessorType = ExportProcessorType.Batch;
                        options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
                    })
                    .IncludeScopes = true;
            });
    });
    return services;
}

I would like now to introduce Serilog to make use of additional features it offers. Is it possible to maintain my configuration centralized by only add the necessary code to this method?

I am currently using a centralized configuration for OpenTelemetry logging. This configuration is then called at startup from every single service directly in Program.cs

With this configuration I am able to send every log to an otel collector instance that I have configured.

private static IServiceCollection AddTelemetryLogging(this IServiceCollection services, ISystemConfiguration systemConfiguration) {
    IOpenTelemetryObservabilityConfiguration otelConfiguration = systemConfiguration.GetOtelConfiguration();

    services.AddLogging(loggingBuilder => {
        var resourceBuilder = ResourceBuilder
            .CreateDefault()
            .AddService(otelConfiguration.ServiceName);
        loggingBuilder.ClearProviders();
        loggingBuilder.AddConsole();
        loggingBuilder.AddDebug();
        loggingBuilder
            .AddOpenTelemetry(options => {
                options
                    .SetResourceBuilder(resourceBuilder)
                    .AddOtlpExporter(options => {
                        options.Endpoint = new Uri(otelConfiguration.CollectorUri);
                        options.ExportProcessorType = ExportProcessorType.Batch;
                        options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
                    })
                    .IncludeScopes = true;
            });
    });
    return services;
}

I would like now to introduce Serilog to make use of additional features it offers. Is it possible to maintain my configuration centralized by only add the necessary code to this method?

Share Improve this question edited Feb 17 at 12:38 Mammt asked Feb 17 at 12:30 MammtMammt 571 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You could use Serilog.Sinks.OpenTelemetry https://github/serilog/serilog-sinks-opentelemetry

    private static IServiceCollection AddTelemetryLogging(this IServiceCollection services, ISystemConfiguration systemConfiguration)
    {
        IOpenTelemetryObservabilityConfiguration otelConfiguration = systemConfiguration.GetOtelConfiguration();
     
        // Create Serilog Logger
        Log.Logger = new LoggerConfiguration()
            .WriteTo.Console()
            .WriteTo.Debug()
            .WriteTo.OpenTelemetry(options =>
            {
                options.Endpoint = new Uri(otelConfiguration.CollectorUri);
                options.Protocol = OtlpProtocol.Grpc;
            })
            .CreateLogger();
        services.AddSerilog(Log.Logger);

        return services;
    }

(Also installed Serilog.Sinks.Console Serilog.AspNetCore

发布评论

评论列表(0)

  1. 暂无评论