1 //------------------------------------------------------------------------------
2 // <copyright file="TraceEventCache.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
8 using System.Threading;
9 using System.Security.Permissions;
11 using System.Collections;
12 using System.Globalization;
13 using System.Runtime.Versioning;
15 namespace System.Diagnostics {
16 public class TraceEventCache {
18 private static volatile int processId;
19 private static volatile string processName;
21 private long timeStamp = -1;
22 private DateTime dateTime = DateTime.MinValue;
23 private string stackTrace = null;
25 internal Guid ActivityId {
26 get { return Trace.CorrelationManager.ActivityId; }
29 public string Callstack {
31 if (stackTrace == null)
32 stackTrace = Environment.StackTrace;
35 new EnvironmentPermission(PermissionState.Unrestricted).Demand();
41 public Stack LogicalOperationStack {
43 return Trace.CorrelationManager.LogicalOperationStack;
47 public DateTime DateTime {
49 if (dateTime == DateTime.MinValue)
50 dateTime = DateTime.UtcNow;
55 public int ProcessId {
56 [ResourceExposure(ResourceScope.Process)] // Returns the current process's pid
57 [ResourceConsumption(ResourceScope.Process)]
59 return GetProcessId();
63 public string ThreadId {
65 return GetThreadId().ToString(CultureInfo.InvariantCulture);
69 public long Timestamp {
72 timeStamp = Stopwatch.GetTimestamp();
77 [ResourceExposure(ResourceScope.None)]
78 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
79 private static void InitProcessInfo() {
81 // Demand unmanaged code permission
82 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
84 if (processName == null) {
85 Process p = Process.GetCurrentProcess();
88 processName = p.ProcessName;
96 [ResourceExposure(ResourceScope.Process)]
97 internal static int GetProcessId() {
102 internal static string GetProcessName() {
107 internal static int GetThreadId() {
108 return Thread.CurrentThread.ManagedThreadId;