.NET logs installation
- 1
Install OpenTelemetry packages
RequiredFor the complete SDK reference, see the OpenTelemetry .NET docs.
TerminalOpenTelemetry.Extensions.Hostingwires OpenTelemetry into the host builder. For a plain console app that builds its ownLoggerFactory, you can leave it out. - 2
Get your project token
RequiredYou'll need your PostHog project token to authenticate log requests. This is the same key you use for capturing events and exceptions with the PostHog SDK.
Important: Use your project token which starts with
phc_. Do not use a personal API key (which starts withphx_).You can find your project token in Project settings.
- 3
Configure the SDK
RequiredPoint the OpenTelemetry logging provider at PostHog. This attaches to the standard
ILoggerpipeline, so anything already logging throughILoggeris exported without changing your call sites.C#Note: With
HttpProtobuf, theEndpointis used as-is, so include the full/i/v1/logspath. Don't use the baseOTEL_EXPORTER_OTLP_ENDPOINTvariable, which appends its own/v1/logs.Alternatively, configure the exporter with environment variables and call
AddOtlpExporter()with no arguments:TerminalYou can also pass the project token as a query parameter instead of a header, though it will then appear in proxy and CDN access logs:
C#For a console app or any process without a host builder, create the logger factory directly and keep it alive for the lifetime of the process — disposing it flushes pending logs:
C# - 4
Use ILogger
RequiredLog through
ILoggeras usual. Message template placeholders become searchable attributes, so pass structured values as parameters rather than interpolating them into the string:C#Important:
logger.LogInformation($"User {userId} logged in")produces a different message body on every call and no attributes. Use the template form above instead.To link logs to a person and their session replay, add the identifiers as a scope. These keys are matched exactly, so
posthog_distinct_idand other snake_case variants won't link:C#Scope values are only exported when
IncludeScopes = trueis set, as in the configuration above. - 5
Next steps
CheckpointWhat you can do with your logsAction Description Why you need logs What logs show you that nothing else does Search logs Use the search interface to find specific log entries Filter by level Filter by INFO,WARN,ERROR, etc.Link session replay Connect logs to users and session replays by passing posthogDistinctIdandsessionIdLink logs to a person Surface every log emitted on behalf of a user on their PostHog person profile Logging best practices Learn what to log, how to structure logs, and patterns that make logs useful in production