Activity
Activity represents operation with context to be used for logging.
Activity has operation name, Id, start time and duration, tags and baggage.
Current activity can be accessed with static AsyncLocal variable Activity.Current.
Activities should be created with constructor, configured as necessary
and then started with Activity.Start method which maintains parent-child
relationships for the activities and sets Activity.Current.
When activity is finished, it should be stopped with static Activity.Stop method.
No methods on Activity allow exceptions to escape as a response to bad inputs.
They are thrown and caught (that allows Debuggers and Monitors to see the error)
but the exception is suppressed, and the operation does something reasonable (typically
doing nothing).
Gets or sets the current operation (Activity) for the current thread. This flows
across async calls.
Activity tries to use the same format for IDs as its parent.
However if the activity has no parent, it has to do something.
This determines the default format we use.
Normally if the ParentID is defined, the format of that is used to determine the
format used by the Activity. However if ForceDefaultFormat is set to true, the
ID format will always be the DefaultIdFormat even if the ParentID is define and is
a different format.
When starting an Activity which does not have a parent context, the Trace Id will automatically be generated using random numbers.
TraceIdGenerator can be used to override the runtime's default Trace Id generation algorithm.
Return the flags (defined by the W3C ID specification) associated with the activity.
Baggage is string-string key-value pairs that represent information that will
be passed along to children of this activity. Baggage is serialized
when requests leave the process (along with the ID). Typically Baggage is
used to do fine-grained control over logging of the activity and any children.
In general, if you are not using the data at runtime, you should be using Tags
instead.
Get the context of the activity. Context becomes valid only if the activity has been started.
otherwise will default context.
Gets or sets the display name of the Activity
If the Activity has ended ( Stop or SetEndTime was called) then this is the delta
between StartTimeUtc and end. If Activity is not ended and SetEndTime was not called then this is
Zero.
Events is the list of all ActivityEvent objects attached to this Activity object.
If there is not any ActivityEvent object attached to the Activity object, Events will return empty list.
This is an ID that is specific to a particular request. Filtering
to a particular ID insures that you get only one request that matches.
Id has a hierarchical structure: '|root-id.id1_id2.id3_' Id is generated when
Start is called by appending suffix to Parent.Id
or ParentId; Activity has no Id until it started
See for more details
Returns the format for the ID.
Indicate if the this Activity object should be populated with all the propagation info and also all other
properties such as Links, Tags, and Events.
Gets the relationship between the Activity, its parents, and its children in a Trace.
Links is the list of all ActivityLink objects attached to this Activity object.
If there is no any ActivityLink object attached to the Activity object, Links will return empty list.
An operation name is a COARSEST name that is useful grouping/filtering.
The name is typically a compile-time constant. Names of Rest APIs are
reasonable, but arguments (e.g. specific accounts etc), should not be in
the name but rather in the tags.
If the Activity that created this activity is from the same process you can get
that Activity with Parent. However, this can be null if the Activity has no
parent (a root activity) or if the Parent is from outside the process.
If the parent for this activity comes from outside the process, the activity
does not have a Parent Activity but MAY have a ParentId (which was deserialized from
from the parent). This accessor fetches the parent ID if it exists at all.
Note this can be null if this is a root Activity (it has no parent)
See for more details
If the parent Activity ID has the W3C format, this returns the ID for the SpanId part of the ParentId.
Otherwise it returns a zero SpanId.
True if the W3CIdFlags.Recorded flag is set.
Root Id is substring from Activity.Id (or ParentId) between '|' (or beginning) and first '.'.
Filtering by root Id allows to find all Activities involved in operation processing.
RootId may be null if Activity has neither ParentId nor Id.
See for more details
Get the ActivitySource object associated with this Activity.
If the Activity has the W3C format, this returns the ID for the SPAN part of the Id.
Otherwise it returns a zero SpanId.
The time that operation started. It will typically be initialized when Start
is called, but you can set at any time via SetStartTime.
Gets status code of the current activity object.
Gets the status description of the current activity object.
List of the tags which represent information that will be logged along with the Activity to the logging system.
This information however is NOT passed on to the children of this activity.
Tags are string-string key-value pairs that represent information that will
be logged along with the Activity to the logging system. This information
however is NOT passed on to the children of this activity.
If the Activity has the W3C format, this returns the ID for the TraceId part of the Id.
Otherwise it returns a zero TraceId.
Holds the W3C 'tracestate' header as a string.
Tracestate is intended to carry information supplemental to trace identity contained
in traceparent. List of key value pairs carried by tracestate convey information
about request position in multiple distributed tracing graphs. It is typically used
by distributed tracing systems and should not be used as a general purpose baggage
as this use may break correlation of a distributed trace.
Logically it is just a kind of baggage (if flows just like baggage), but because
it is expected to be special cased (it has its own HTTP header), it is more
convenient/efficient if it is not lumped in with other baggage.
Note that Activity has a 'builder' pattern, where you call the constructor, a number of 'Set*' and 'Add*' APIs and then
call Start to build the activity. You MUST call Start before using it.
Update the Activity to have baggage with an additional 'key' and value 'value'.
This shows up in the Baggage enumeration as well as the GetBaggageItem
method.
Baggage is meant for information that is needed for runtime control. For information
that is simply useful to show up in the log with the activity use Tags.
Returns 'this' for convenient chaining.
Add ActivityEvent object to the Events list.
Update the Activity to have a tag with an additional 'key' and value 'value'.
This shows up in the Tags enumeration. It is meant for information that
is useful to log but not needed for runtime control (for the latter, Baggage)
Update the Activity to have a tag with an additional 'key' and value 'value'.
This shows up in the TagObjects enumeration. It is meant for information that
is useful to log but not needed for runtime control (for the latter, Baggage)
Dispose will stop the Activity if it is already started and notify any event listeners. Nothing will happen otherwise.
Returns the value of the key-value pair added to the activity with AddBaggage.
Returns null if that key does not exist.
GetCustomProperty retrieve previously attached object mapped to the property name.
Returns the value of the Activity tag mapped to the input key/>.
Returns null if that key does not exist.
Add or update the Activity baggage with the input key and value.
If the input value is null
- if the collection has any baggage with the same key, then this baggage will get removed from the collection.
- otherwise, nothing will happen and the collection will not change.
If the input value is not null
- if the collection has any baggage with the same key, then the value mapped to this key will get updated with the new input value.
- otherwise, the key and value will get added as a new baggage to the collection.
SetCustomProperty allow attaching any custom object to this Activity object.
If the property name was previously associated with other object, SetCustomProperty will update to use the new propert value instead.
Sets IdFormat on the Activity before it is started. It takes precedence over
Parent.IdFormat, ParentId format, DefaultIdFormat and ForceDefaultIdFormat.
Updates the Activity To indicate that the activity with ID parentId
caused this activity. This is intended to be used only at 'boundary'
scenarios where an activity from another process logically started
this activity. The Parent ID shows up the Tags (as well as the ParentID
property), and can be used to reconstruct the causal tree.
Returns 'this' for convenient chaining.
public Activity SetParentId(ActivityTraceId traceId, ActivitySpanId spanId, ActivityTraceFlags activityTraceFlags = 0)
Set the parent ID using the W3C convention using a TraceId and a SpanId. This
constructor has the advantage that no string manipulation is needed to set the ID.
Update the Activity to set start time
Sets the status code and description on the current activity object.
Add or update the Activity tag with the input key and value.
If the input value is null
- if the collection has any tag with the same key, then this tag will get removed from the collection.
- otherwise, nothing will happen and the collection will not change.
If the input value is not null
- if the collection has any tag with the same key, then the value mapped to this key will get updated with the new input value.
- otherwise, the key and value will get added as a new tag to the collection.
Starts activity
Sets Parent to hold Current.Sets Current to this activity.If StartTimeUtc was not set previously, sets it to UtcNow.Generates a unique Id for this activity.
Use StartActivity to start activity and write start event.
Stops activity: sets Current to Parent.
If end time was not set previously, sets Duration as a difference between UtcNow and StartTimeUtc
Use StopActivity to stop activity and write stop event.