[bcl] Make mono cas opt-in features instead of opt-out for mobile profiles
[mono.git] / mcs / class / referencesource / System / compmod / system / diagnostics / TraceEventCache.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="TraceEventCache.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 using System;
8 using System.Threading;
9 using System.Security.Permissions;
10 using System.Text;
11 using System.Collections;
12 using System.Globalization;
13 using System.Runtime.Versioning;
14
15 namespace System.Diagnostics {
16     public class TraceEventCache {
17
18         private static volatile int processId;
19         private static volatile string processName;
20
21         private long timeStamp = -1;
22         private DateTime dateTime = DateTime.MinValue;
23         private string stackTrace = null;
24
25         internal Guid ActivityId {
26             get { return Trace.CorrelationManager.ActivityId; }
27         }
28         
29         public string Callstack {
30             get {
31                 if (stackTrace == null)
32                     stackTrace = Environment.StackTrace;
33 #if FEATURE_MONO_CAS
34                 else
35                     new EnvironmentPermission(PermissionState.Unrestricted).Demand();
36 #endif
37                 return stackTrace;
38             }
39         }
40
41         public Stack LogicalOperationStack {
42             get {
43                 return Trace.CorrelationManager.LogicalOperationStack;
44             }
45         }
46
47         public DateTime DateTime {
48             get {
49                 if (dateTime == DateTime.MinValue)
50                     dateTime = DateTime.UtcNow;
51                 return dateTime;
52             }
53         }
54
55         public int ProcessId {
56             [ResourceExposure(ResourceScope.Process)]  // Returns the current process's pid
57             [ResourceConsumption(ResourceScope.Process)]
58             get {
59                 return GetProcessId();
60             }
61         }
62
63         public string ThreadId {
64             get {
65                 return GetThreadId().ToString(CultureInfo.InvariantCulture);
66             }
67         }
68
69         public long Timestamp {
70             get {
71                 if (timeStamp == -1)
72                     timeStamp = Stopwatch.GetTimestamp();
73                 return timeStamp ;
74             }
75         }
76
77         [ResourceExposure(ResourceScope.None)]
78         [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
79         private static void InitProcessInfo() {
80 #if FEATURE_MONO_CAS
81             // Demand unmanaged code permission
82             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
83 #endif
84             if (processName == null) {
85                 Process p = Process.GetCurrentProcess();
86                 try {
87                     processId = p.Id;
88                     processName = p.ProcessName;
89                 }
90                 finally {
91                     p.Dispose();
92                 }
93             }
94         }
95
96         [ResourceExposure(ResourceScope.Process)]
97         internal static int GetProcessId() {
98             InitProcessInfo();
99             return processId;
100         }
101         
102         internal static string GetProcessName() {
103             InitProcessInfo();
104             return processName;
105         }
106         
107         internal static int GetThreadId() {
108             return Thread.CurrentThread.ManagedThreadId;
109         }
110     }
111 }
112