Set Up Logs
Structured logs allow you to send, view and query logs sent from your applications within Sentry.
With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
Logs for .NET are supported in Sentry .NET SDK version 5.14.0
and above.
To enable logging, you need to initialize the SDK with the Experimental.EnableLogs
option set to true
.
SentrySdk.Init(options =>
{
options.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
// Enable logs to be sent to Sentry
options.Experimental.EnableLogs = true;
});
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the SentrySdk.Experimental.Logger
APIs.
The SentrySdk.Experimental.Logger
instance exposes six methods that you can use to log messages at different log levels: Trace
, Debug
, Info
, Warning
, Error
, and Fatal
.
These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
SentrySdk.Experimental.Logger.LogInfo("A simple log message");
SentrySdk.Experimental.Logger.LogError("A {0} log message", ["formatted"]);
Note
During the experimental phase of the feature, we will provide more method overloads for convenient invocation in common scenarios. Additionally, we may provide method overloads that are not based on composite format strings, but on interpolated strings.
The SDK automatically provides a set of default attributes attached to your logs. Additionally, you can attach custom attributes via a delegate.
SentrySdk.Experimental.Logger.LogWarning("A log message with additional attributes.", [], static log =>
{
log.SetAttribute("my.attribute", "value");
});
Note
Please note that we will revise the API shape to set custom attributes during the experimental phase of the feature.
Supported attribute types are:
- Textual:
string
andchar
- Logical:
bool
- Integral:
sbyte
,byte
,short
,ushort
,int
,uint
,long
andnint
- Floating-point:
float
anddouble
Unsupported numeric types such as ulong
, nuint
, decimal
, as well as all other types including object
, are treated as string
via ToString()
.
Available integrations:
If there's an integration you would like to see, open a new issue on GitHub.
Set to true
in order to enable the SentrySdk.Experimental.Logger
APIs, as well as logging integrations via the ILogger<TCategoryName>
API.
To filter logs, or update them before they are sent to Sentry, you can use the Experimental.SetBeforeSendLog(Func<SentryLog, SentryLog?>)
option.
options =>
{
options.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
options.Experimental.EnableLogs = true;
// a callback that is invoked before sending a log to Sentry
options.Experimental.SetBeforeSendLog(static log =>
{
// filter out all info logs
if (log.Level is SentryLogLevel.Info)
{
return null;
}
// filter out logs based on some attribute they have
if (log.TryGetAttribute("suppress", out var attribute) && attribute is true)
{
return null;
}
// set a custom attribute for all other logs sent to Sentry
log.SetAttribute("my.attribute", "value");
return log;
});
});
The callback function set via Experimental.SetBeforeSendLog(Func<SentryLog, SentryLog?>)
receives a log object, and should return the log object if you want it to be sent to Sentry, or null
if you want to discard it.
The log object of type SentryLog
has the following members:
Timestamp
Property: (DateTimeOffset
) The timestamp of the log.TraceId
Property: (SentryId
) The trace id of the log.Level
Property: (SentryLogLevel
) The severity level of the log. EitherTrace
,Debug
,Info
,Warning
,Error
, orFatal
.Message
Property: (string
) The formatted log message.Template
Property: (string?
) The parameterized template string.Parameters
Property: (ImmutableArray<KeyValuePair<string, object>>
) The parameters to the template string.ParentSpanId
Property: (SpanId?
) The span id of the span that was active when the log was collected.TryGetAttribute(string key, out object value)
Method: Gets the attribute value associated with the specified key. Returnstrue
if the log contains an attribute with the specified key and it's value is notnull
, otherwisefalse
.SetAttribute(string key, object value)
Method: Sets a key-value pair of data attached to the log. Supported types arestring
,bool
, integers up to a size of 64-bit signed, and floating-point numbers up to a size of 64-bit.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").