-
Notifications
You must be signed in to change notification settings - Fork 765
/
LogRecordData.cs
141 lines (127 loc) · 4.43 KB
/
LogRecordData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
#if NET && EXPOSE_EXPERIMENTAL_FEATURES
using System.Diagnostics.CodeAnalysis;
using OpenTelemetry.Internal;
#endif
namespace OpenTelemetry.Logs;
#if EXPOSE_EXPERIMENTAL_FEATURES
/// <summary>
/// Stores details about a log message.
/// </summary>
/// <remarks><inheritdoc cref="Logger" path="/remarks"/></remarks>
#if NET
[Experimental(DiagnosticDefinitions.LogsBridgeExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)]
#endif
public
#else
/// <summary>
/// Stores details about a log message.
/// </summary>
internal
#endif
struct LogRecordData
{
internal DateTime TimestampBacking = DateTime.UtcNow;
/// <summary>
/// Initializes a new instance of the <see cref="LogRecordData"/> struct.
/// </summary>
/// <remarks>
/// Notes:
/// <list type="bullet">
/// <item>The <see cref="Timestamp"/> property is initialized to <see
/// cref="DateTime.UtcNow"/> automatically.</item>
/// <item>The <see cref="TraceId"/>, <see cref="SpanId"/>, and <see
/// cref="TraceFlags"/> properties will be set using the <see
/// cref="Activity.Current"/> instance.</item>
/// </list>
/// </remarks>
public LogRecordData()
: this(Activity.Current?.Context ?? default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LogRecordData"/> struct.
/// </summary>
/// <remarks>
/// Note: The <see cref="Timestamp"/> property is initialized to <see
/// cref="DateTime.UtcNow"/> automatically.
/// </remarks>
/// <param name="activity">Optional <see cref="Activity"/> used to populate
/// trace context properties (<see cref="TraceId"/>, <see cref="SpanId"/>,
/// and <see cref="TraceFlags"/>).</param>
public LogRecordData(Activity? activity)
: this(activity?.Context ?? default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LogRecordData"/> struct.
/// </summary>
/// <remarks>
/// Note: The <see cref="Timestamp"/> property is initialized to <see
/// cref="DateTime.UtcNow"/> automatically.
/// </remarks>
/// <param name="activityContext"><see cref="ActivityContext"/> used to
/// populate trace context properties (<see cref="TraceId"/>, <see
/// cref="SpanId"/>, and <see cref="TraceFlags"/>).</param>
public LogRecordData(in ActivityContext activityContext)
{
this.TraceId = activityContext.TraceId;
this.SpanId = activityContext.SpanId;
this.TraceFlags = activityContext.TraceFlags;
}
/// <summary>
/// Gets or sets the log timestamp.
/// </summary>
/// <remarks>
/// Note: If <see cref="Timestamp"/> is set to a value with <see
/// cref="DateTimeKind.Local"/> it will be automatically converted to
/// UTC using <see cref="DateTime.ToUniversalTime"/>.
/// </remarks>
public DateTime Timestamp
{
readonly get => this.TimestampBacking;
set { this.TimestampBacking = value.Kind == DateTimeKind.Local ? value.ToUniversalTime() : value; }
}
/// <summary>
/// Gets or sets the log <see cref="ActivityTraceId"/>.
/// </summary>
public ActivityTraceId TraceId { get; set; }
/// <summary>
/// Gets or sets the log <see cref="ActivitySpanId"/>.
/// </summary>
public ActivitySpanId SpanId { get; set; }
/// <summary>
/// Gets or sets the log <see cref="ActivityTraceFlags"/>.
/// </summary>
public ActivityTraceFlags TraceFlags { get; set; }
/// <summary>
/// Gets or sets the original string representation of the severity as it is
/// known at the source.
/// </summary>
public string? SeverityText { get; set; } = null;
/// <summary>
/// Gets or sets the log severity.
/// </summary>
public LogRecordSeverity? Severity { get; set; } = null;
/// <summary>
/// Gets or sets the log body.
/// </summary>
public string? Body { get; set; } = null;
internal static void SetActivityContext(ref LogRecordData data, Activity? activity)
{
if (activity != null)
{
data.TraceId = activity.TraceId;
data.SpanId = activity.SpanId;
data.TraceFlags = activity.ActivityTraceFlags;
}
else
{
data.TraceId = default;
data.SpanId = default;
data.TraceFlags = ActivityTraceFlags.None;
}
}
}