Dotnet core WPF application - how to get APM traces

Hello all,
I have a .net core WPF application (not asp.net core). I'm also using Serilog for logging.
From Elastic Stack: Fleet with integrations: APM 8.4.2, Custom Log 1.0.0, System 1.16.2
Also using Elastic-agent 8.4 running on my laptop

I can't seem to get APM trace ids in my logs. Is this possible for a none asp.net application?

My setup looks like this

private IHostBuilder CreateHostBuilder()
        {
            return Host.CreateDefaultBuilder()
                .UseAllElasticApm()
                .ConfigureServices((hostContext, services) =>
                {
                    //Add you business services
                    services.AddLogging(loggingBuilder =>
                        loggingBuilder
                            .AddSerilog(dispose: true)
                            .AddConsole());
                    services.AddScoped<MainWindow>();
                    services.AddScoped<MainWindowViewModel>();
               });
        }

And logger setup

Log.Logger = new LoggerConfiguration()
                .MinimumLevel.ControlledBy(levelSwitch)
                .MinimumLevel.Override("Microsoft.NetCore", LogEventLevel.Warning)
                .MinimumLevel.Override("Elastic.Apm", LogEventLevel.Warning)
                .Enrich.FromLogContext()
                .Enrich.WithElasticApmCorrelationInfo()
                .WriteTo.Console(outputTemplate: "[{ElasticApmTraceId} {ElasticApmTransactionId} {Message:lj} {NewLine}{Exception}")
                .Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
                    .WithDefaultDestructurers())
                .WriteTo.File(new ElasticsearchJsonFormatter(), $"C:\\Temp\\ES\\logES.json", LogEventLevel.Information, fileSizeLimitBytes: 100000, null, false, false, null, RollingInterval.Hour)
                .CreateLogger();

Thank you

Hi @SaEd,

may I ask you to elaborate on your use case and provide me with additional information, please?

  • Are you using the agent's Tracer API (see here) for creating transactions in your WPF application?
    • Or are you using technologies (e.g. HTTP Client, MongoDB) that you expect to be monitored out-of-the-box?
  • Which Elastic NuGet packages are you using in your application?
  • Do see your WPF application show up at all on your APM server or is this problem only about logging enrichment?

Thanks,
Wolfgang

Hi @Wolfgang_Ziegler ,
I would like to use Serilog to capture my code instrumentation. Something similar to

_log.Information("{objectName} {TimeTaken} {Measuring} {Count}  {SPROC}",
                "SomeUtil", sw.ElapsedMilliseconds, "MyMethod",
                myList.Count.ToString(), MY_SPROC);

This shows up in Elastic Search but not in APM. I'm using the Tracer API in my top-level method as in

Elastic.Apm.Agent.Tracer
                .CaptureTransaction("Top Level Thread", ApiConstants.TypeRequest, (t) =>
                {
                    someUtil.MyMethod();
                    _log.Information("{className} {TimeTaken} {Measuring}", GetType().Name, sw.ElapsedMilliseconds, "someUtil.MyMethod");
                });

This one shows up in APM logs and it has the Trace Id populated. But, all other log statements in my "someUtil.MyMethod()" call do not. I was expecting everything within the same transaction to be logged to APM?

Elastic.Apm.NetCoreAll: 1.17.0
Elastic.Apm: 1.17.0
Elastic.Apm.Extensions.Logging: 1.17.0
Elastic.Apm.SqlClient: 1.17.0
Elastic.Apm.SerilogEnricher: 1.5.3
Elastic.CommonSchema.Serilog: 1.5.3
Serilog.Formatting.Elasticsearch: 8.4.1

Hi @SaEd,

I tried to reproduce your problem but unfortunately to no avail.

Are you using the same ILogger object in someUtil.MyMethod as directly in the CaptureTransaction lambda?

Could you try (just for testing) passing the _log object as an argument to someUtil.MyMethod and see if the logs show up then if you use this one?

Another sanity check would be to print the values of:

  • Agent.Tracer.CurrentTransaction.Id
  • Agent.Tracer.CurrentTransaction.TraceId

in all locations where you write logs and expect correlations with APM transactions.

If CurrentTransaction is not null then those logs must be enriched.

Thanks @Wolfgang_Ziegler , all the above is correct and available.
Somehow everything started to work yesterday. I'm not sure what changed.

The logging statement in question is in a sperate dll, so I included a few NuGet packages to the project and that's when I noticed it to work. But, then I removed them all, did a clean build but it is still working fine. I'm puzzled?

Thanks for the update @SaEd,
since we cannot reproduce the issue anymore, I will close this question.
Feel free to reach out again, in case the problem shows up again.
Cheers,
Wolfgang