17 Feb 2025 08:46 AM
We currently log exceptions using Sentry (which we like very much) and an ILogger interface in .Net Core. We also have Dynatrace and would like these exceptions to be available within Dynatrace and be able to track\trace related api calls etc..
These exceptions are data rich exceptions which include:
Im confused on what’s the best way to get these exception into Dynatrace and have a few queries:
Any help or best practice for this would be very helpful.
Solved! Go to Solution.
17 Feb 2025 11:02 AM
1. Data as exception stacktraces and headers/custom info you can log using OneAgent SDK for .NET (https://github.com/Dynatrace/OneAgent-SDK-for-dotnet). It requires manual instrumentation. Depending on type of data you can ingest the mentioned data as logs and corelate it with traces or report as errors.
2.In this case in my opinion is to create a new class that inherits from your Logger implementation that overrides your logger methods and add required headers using OneAgent SDK for .NET. In this case you can log your custom error and attributes which will be corelated with trace.
namespace LicenseManager.WebApi;
public interface ILogger
{
void LogError(string message);
void LogInformation(string message);
}
public class Logger : ILogger
{
public void LogError(string message)
{
Console.WriteLine($"Error: {message}");
}
public void LogInformation(string message)
{
Console.WriteLine($"Info: {message}");
}
}
public class MyLogger : Logger
{
public new void LogError(string message)
{
IOneAgentSdk oneAgentSdk = OneAgentSdkFactory.CreateInstance();
ITraceContextInfo traceContextInfo = oneAgentSdk.TraceContextInfo;
string traceId = traceContextInfo.TraceId;
string spanId = traceContextInfo.SpanId;
string yourCustomError = message.GetError();
dbTracer.Error(yourCustomError);
oneAgentSdk.AddCustomRequestAttribute("region", "EMEA");
oneAgentSdk.AddCustomRequestAttribute("salesAmount", 2500);
oneAgentSdk.AddCustomRequestAttribute("service-quality", 0.707106);
base.LogError($"[!dt dt.trace_id={traceId},dt.span_id={spanId}] {message}");
}
public new void LogInformation(string message)
{
IOneAgentSdk oneAgentSdk = OneAgentSdkFactory.CreateInstance();
ITraceContextInfo traceContextInfo = oneAgentSdk.TraceContextInfo;
string traceId = traceContextInfo.TraceId;
string spanId = traceContextInfo.SpanId;
base.LogInformation($"[!dt dt.trace_id={traceId},dt.span_id={spanId}] {message}");
}
}
Of course you can also add that headers inside of your logger implementation.
Basically, logs are collected by OneAgent and forwarded by it to your Dynatrace tenant/cluster, but it is not corelated with traces. The information mentioned above allows you to corelate traces with logs.