|
| 1 | +--- |
| 2 | +navigation_title: EDOT .NET |
| 3 | +description: Use the information in this section to troubleshoot common problems affecting the {{edot}} .NET. |
| 4 | +applies_to: |
| 5 | + stack: |
| 6 | + serverless: |
| 7 | + observability: |
| 8 | + product: |
| 9 | + edot_dotnet: ga |
| 10 | +products: |
| 11 | + - id: cloud-serverless |
| 12 | + - id: observability |
| 13 | + - id: edot-sdk |
| 14 | +--- |
| 15 | + |
| 16 | +# Troubleshooting the EDOT .NET SDK |
| 17 | + |
| 18 | +Use the information in this section to troubleshoot common problems. As a first step, make sure your stack is compatible with the [supported technologies](opentelemetry://reference/edot-sdks/dotnet/supported-technologies.md) for EDOT .NET and the OpenTelemetry SDK. |
| 19 | + |
| 20 | +If you have an Elastic support contract, create a ticket in the [Elastic Support portal](https://support.elastic.co/customers/s/login/). If you don't, post in the [APM discuss forum](https://discuss.elastic.co/c/apm) or [open a GitHub issue](https://github.com/elastic/elastic-otel-dotnet/issues). |
| 21 | + |
| 22 | +## Obtain EDOT .NET diagnostic logs |
| 23 | + |
| 24 | +For most problems, such as when you don't see data in your Elastic Observability backend, first check the EDOT .NET logs. These logs show initialization details and OpenTelemetry SDK events. If you don't see any warnings or errors in the EDOT .NET logs, switch the log level to `Trace` to investigate further. |
| 25 | + |
| 26 | +The {{edot}} .NET includes built-in diagnostic logging. You can direct logs to a file, STDOUT, or, in common scenarios, an `ILogger` instance. EDOT .NET also observes the built-in diagnostics events from the upstream OpenTelemetry SDK and includes those in its logging output. You can collect the log output and use it to diagnose issues locally during development or when working with Elastic support channels. |
| 27 | + |
| 28 | +## ASP.NET Core (generic host) logging integration |
| 29 | + |
| 30 | +When you build applications based on the generic host, such as those created by the [ASP.NET Core](https://learn.microsoft.com/aspnet/core/introduction-to-aspnet-core) and [worker service](https://learn.microsoft.com/dotnet/core/extensions/workers) templates, the {{edot}} .NET will try to automatically register with the built-in logging components when you use the `IHostApplicationBuilder.AddElasticOpenTelemetry` extension method to register EDOT .NET. |
| 31 | + |
| 32 | +```csharp |
| 33 | +var builder = WebApplication.CreateBuilder(args); |
| 34 | +builder.AddElasticOpenTelemetry(); |
| 35 | +``` |
| 36 | + |
| 37 | +In this scenario, EDOT .NET tries to access an available `ILoggerFactory` and create an `ILogger`, logging to the event category `Elastic.OpenTelemetry`. EDOT .NET will register this as the additional logger for its diagnostics unless you have already configured a user-provided `ILogger`. This ensures that EDOT .NET and OpenTelemetry SDK logs are written for your application's configured logging providers. In ASP.NET Core, this includes the console logging provider and results in logs such as the following: |
| 38 | + |
| 39 | +``` |
| 40 | +info: Elastic.OpenTelemetry[0] |
| 41 | + Elastic Distribution of OpenTelemetry (EDOT) .NET: 1.0.0 |
| 42 | +info: Elastic.OpenTelemetry[0] |
| 43 | + EDOT log file: <disabled> |
| 44 | +info: Microsoft.Hosting.Lifetime[14] |
| 45 | + Now listening on: https://localhost:7295 |
| 46 | +info: Microsoft.Hosting.Lifetime[14] |
| 47 | + Now listening on: http://localhost:5247 |
| 48 | +info: Microsoft.Hosting.Lifetime[0] |
| 49 | + Application started. Press Ctrl+C to shut down. |
| 50 | +info: Microsoft.Hosting.Lifetime[0] |
| 51 | + Hosting environment: Development |
| 52 | +``` |
| 53 | + |
| 54 | +In the preceding log output, informational level logging is enabled as the default for this application. You can control the output by configuring the log levels. |
| 55 | + |
| 56 | +### Configuring the log level |
| 57 | + |
| 58 | +You can [configure](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#configure-logging) logs sent to the integrated `Microsoft.Extensions.Logging` library in several ways. A common choice is to use the `appsettings.json` file to configure log-level filters for specific categories. |
| 59 | + |
| 60 | +```json |
| 61 | +{ |
| 62 | + "Logging": { |
| 63 | + "LogLevel": { |
| 64 | + "Default": "Information", |
| 65 | + "Microsoft.AspNetCore": "Warning", |
| 66 | + "Elastic.OpenTelemetry": "Warning" |
| 67 | + } |
| 68 | + }, |
| 69 | + "AllowedHosts": "*" |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +In the preceding code, you have filtered `Elastic.OpenTelemetry` to only emit log entries when they have the `Warning` log level or a higher severity. This overrides the `Default` configuration of `Information`. |
| 74 | + |
| 75 | +## Enable global file logging |
| 76 | + |
| 77 | +Integrated logging is helpful because it requires little to no setup. The logging infrastructure is not present by default in some application types, such as console applications. EDOT .NET also offers a global file logging feature, which is the easiest way for you to get diagnostics and debug information. You must enable file logging when you work with Elastic support, as trace logs will be requested. |
| 78 | + |
| 79 | +Specify at least one of the following environment variables to make sure that EDOT .NET logs into a file. |
| 80 | + |
| 81 | +`OTEL_LOG_LEVEL` _(optional)_: |
| 82 | +Set the log level at which the profiler should log. Valid values are |
| 83 | + |
| 84 | +* trace |
| 85 | +* debug |
| 86 | +* information |
| 87 | +* warning |
| 88 | +* error |
| 89 | +* none |
| 90 | + |
| 91 | +The default value is `information`. More verbose log levels like `trace` and `debug` can affect the runtime performance of profiler auto instrumentation, so use them _only_ for diagnostics purposes. |
| 92 | + |
| 93 | +:::{note} |
| 94 | +If you don't explicitly set `ELASTIC_OTEL_LOG_TARGETS` to include `file`, global file logging will only be enabled when you configure it with `trace` or `debug`. |
| 95 | +::: |
| 96 | + |
| 97 | +`OTEL_DOTNET_AUTO_LOG_DIRECTORY` _(optional)_: |
| 98 | +Set the directory in which to write log files. If you don't set this, the default is: |
| 99 | + |
| 100 | +* `%USERPROFILE%\AppData\Roaming\elastic\elastic-otel-dotnet` on Windows |
| 101 | +* `/var/log/elastic/elastic-otel-dotnet` on Linux |
| 102 | +* `~/Library/Application Support/elastic/elastic-otel-dotnet` on OSX |
| 103 | + |
| 104 | +> ::::{important} |
| 105 | +> Make sure the user account under which the profiler process runs has permission to write to the destination log directory. Specifically, when you run on IIS, ensure that the [AppPool identity](https://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities) has write permissions in the target directory. |
| 106 | +> :::: |
| 107 | +
|
| 108 | +`ELASTIC_OTEL_LOG_TARGETS` _(optional)_: |
| 109 | +A semi-colon separated list of targets for profiler logs. Valid values are |
| 110 | + |
| 111 | +* file |
| 112 | +* stdout |
| 113 | +* none |
| 114 | + |
| 115 | +The default value is `file` if you set `OTEL_DOTNET_AUTO_LOG_DIRECTORY` or set `OTEL_LOG_LEVEL` to `trace` or `debug`. |
| 116 | + |
| 117 | +## Advanced troubleshooting |
| 118 | + |
| 119 | +### Diagnosing initialization or bootstrap issues |
| 120 | + |
| 121 | +If EDOT for .NET fails before fully bootstrapping its internal components, it won't generate a log file. In such circumstances, you can provide an additional logger for diagnostic purposes. Alternatively, you can enable the `STDOUT` log target. |
| 122 | + |
| 123 | +#### Providing an additional application logger |
| 124 | + |
| 125 | +You can provide an additional `ILogger` that EDOT .NET will use to log pre-bootstrap events by creating an instance of `ElasticOpenTelemetryOptions`. |
| 126 | + |
| 127 | +```csharp |
| 128 | +using Elastic.OpenTelemetry; |
| 129 | +using Microsoft.Extensions.Logging; |
| 130 | +using OpenTelemetry; |
| 131 | + |
| 132 | +using ILoggerFactory loggerFactory = LoggerFactory.Create(static builder => |
| 133 | +{ |
| 134 | + builder |
| 135 | + .AddFilter("Elastic.OpenTelemetry", LogLevel.Trace) |
| 136 | + .AddConsole(); |
| 137 | +}); |
| 138 | + |
| 139 | +ILogger logger = loggerFactory.CreateLogger("EDOT"); |
| 140 | + |
| 141 | +var options = new ElasticOpenTelemetryOptions |
| 142 | +{ |
| 143 | + AdditionalLogger = logger |
| 144 | +}; |
| 145 | + |
| 146 | +using var sdk = OpenTelemetrySdk.Create(builder => builder |
| 147 | + .WithElasticDefaults(options)); |
| 148 | +``` |
| 149 | + |
| 150 | +This example adds the console logging provider, but you can include any provider here. To use this sample code, add a dependency on the `Microsoft.Extensions.Logging.Console` [NuGet package](https://www.nuget.org/packages/microsoft.extensions.logging.console). |
| 151 | + |
| 152 | +You create and configure an `ILoggerFactory`. In this example, you configure the `Elastic.OpenTelemetry` category to capture trace logs, which is the most verbose option. This is the best choice when you diagnose initialization issues. |
| 153 | + |
| 154 | +You use the `ILoggerFactory` to create an `ILogger`, which you then assign to the `ElasticOpenTelemetryOptions.AdditionalLogger` property. Once you pass the `ElasticOpenTelemetryOptions` into the `WithElasticDefaults` method, the provided logger can capture bootstrap logs. |
| 155 | + |
| 156 | +To simplify the preceding code, you can also configure the `ElasticOpenTelemetryOptions` with an `ILoggerFactory` instance that EDOT .NET can use to create its own logger. |
| 157 | + |
| 158 | +```csharp |
| 159 | +using var loggerFactory = LoggerFactory.Create(static builder => |
| 160 | +{ |
| 161 | + builder |
| 162 | + .AddFilter("Elastic.OpenTelemetry", LogLevel.Debug) |
| 163 | + .AddConsole(); |
| 164 | +}); |
| 165 | + |
| 166 | +var options = new ElasticOpenTelemetryOptions |
| 167 | +{ |
| 168 | + AdditionalLoggerFactory = loggerFactory |
| 169 | +}; |
| 170 | + |
| 171 | +using var sdk = OpenTelemetrySdk.Create(builder => builder |
| 172 | + .WithElasticDefaults(options)); |
| 173 | +``` |
| 174 | + |
| 175 | +## Known issues |
| 176 | + |
| 177 | +The following known issues affect EDOT .NET. |
| 178 | + |
| 179 | +### Missing log records |
| 180 | + |
| 181 | +The upstream SDK currently does not [comply with the spec](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4324) regarding the deduplication of attributes when exporting log records. When you create a log within multiple scopes, each scope may store information using the same logical key. In this situation, the exported data will have duplicated attributes. |
| 182 | + |
| 183 | +You are most likely to see this when you log in the scope of a request and enable the `OpenTelemetryLoggerOptions.IncludeScopes` option. ASP.NET Core adds the `RequestId` to multiple scopes. We recommend that you don't enable `IncludeScopes` until the SDK fixes this. When you use the EDOT Collector or the [{{motlp}}](opentelemetry://reference/motlp.md) in serverless, non-compliant log records will fail to be ingested. |
| 184 | + |
| 185 | +EDOT .NET currently emits a warning if it detects that you use `IncludeScopes` in ASP.NET Core scenarios. |
| 186 | + |
| 187 | +This can also happen even when you set `IncludeScopes` to false. The following code will also result in duplicate attributes and the potential for lost log records. |
| 188 | + |
| 189 | +```csharp |
| 190 | +Logger.LogInformation("Eat your {fruit} {fruit} {fruit}!", "apple", "banana", "mango"); |
| 191 | +``` |
| 192 | + |
| 193 | +To avoid this scenario, make sure each placeholder uses a unique name. For example: |
| 194 | + |
| 195 | +```csharp |
| 196 | +Logger.LogInformation("Eat your {fruit1} {fruit2} {fruit3}!", "apple", "banana", "mango"); |
| 197 | +``` |
0 commit comments