PageRenderTime 101ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/referencesource/System.Core/System/Diagnostics/Eventing/Reader/EventLogRecord.cs

https://github.com/pruiz/mono
C# | 394 lines | 303 code | 62 blank | 29 comment | 38 complexity | ad2000ef840d2529767ed6c4cad4d4c8 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // ==++==
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // ==--==
  6. /*============================================================
  7. **
  8. ** Class: EventLogRecord
  9. **
  10. ** Purpose:
  11. ** This public class is an EventLog implementation of EventRecord. An
  12. ** instance of this is obtained from an EventLogReader.
  13. **
  14. ============================================================*/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. using System.Security.Permissions;
  20. using Microsoft.Win32;
  21. namespace System.Diagnostics.Eventing.Reader {
  22. [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
  23. public class EventLogRecord : EventRecord {
  24. private const int SYSTEM_PROPERTY_COUNT = 18;
  25. //
  26. // access to the data member reference is safe, while
  27. // invoking methods on it is marked SecurityCritical as appropriate.
  28. //
  29. [System.Security.SecuritySafeCritical]
  30. private EventLogHandle handle;
  31. private EventLogSession session;
  32. private NativeWrapper.SystemProperties systemProperties;
  33. private string containerChannel;
  34. private int[] matchedQueryIds;
  35. //a dummy object which is used only for the locking.
  36. object syncObject;
  37. //cached DisplayNames for each instance
  38. private string levelName = null;
  39. private string taskName = null;
  40. private string opcodeName = null;
  41. private IEnumerable<string> keywordsNames = null;
  42. //cached DisplayNames for each instance
  43. private bool levelNameReady;
  44. private bool taskNameReady;
  45. private bool opcodeNameReady;
  46. private ProviderMetadataCachedInformation cachedMetadataInformation;
  47. // marking as TreatAsSafe because just passing around a reference to an EventLogHandle is safe.
  48. [System.Security.SecuritySafeCritical]
  49. internal EventLogRecord(EventLogHandle handle, EventLogSession session, ProviderMetadataCachedInformation cachedMetadataInfo) {
  50. this.cachedMetadataInformation = cachedMetadataInfo;
  51. this.handle = handle;
  52. this.session = session;
  53. systemProperties = new NativeWrapper.SystemProperties();
  54. syncObject = new object();
  55. }
  56. internal EventLogHandle Handle {
  57. // just returning reference to security critical type, the methods
  58. // of that type are protected by SecurityCritical as appropriate.
  59. [System.Security.SecuritySafeCritical]
  60. get {
  61. return handle;
  62. }
  63. }
  64. internal void PrepareSystemData() {
  65. if (this.systemProperties.filled)
  66. return;
  67. //prepare the System Context, if it is not already initialized.
  68. this.session.SetupSystemContext();
  69. lock (this.syncObject) {
  70. if (this.systemProperties.filled == false) {
  71. NativeWrapper.EvtRenderBufferWithContextSystem(this.session.renderContextHandleSystem, this.handle, UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventValues, this.systemProperties, SYSTEM_PROPERTY_COUNT);
  72. this.systemProperties.filled = true;
  73. }
  74. }
  75. }
  76. public override int Id {
  77. get {
  78. PrepareSystemData();
  79. if (this.systemProperties.Id == null)
  80. return 0;
  81. return (int)this.systemProperties.Id;
  82. }
  83. }
  84. public override byte? Version {
  85. get {
  86. PrepareSystemData();
  87. return this.systemProperties.Version;
  88. }
  89. }
  90. public override int? Qualifiers {
  91. get {
  92. PrepareSystemData();
  93. return (int?)(uint?)this.systemProperties.Qualifiers;
  94. }
  95. }
  96. public override byte? Level {
  97. get {
  98. PrepareSystemData();
  99. return this.systemProperties.Level;
  100. }
  101. }
  102. public override int? Task {
  103. get {
  104. PrepareSystemData();
  105. return (int?)(uint?)this.systemProperties.Task;
  106. }
  107. }
  108. public override short? Opcode {
  109. get {
  110. PrepareSystemData();
  111. return (short?)(ushort?)this.systemProperties.Opcode;
  112. }
  113. }
  114. public override long? Keywords {
  115. get {
  116. PrepareSystemData();
  117. return (long?)this.systemProperties.Keywords;
  118. }
  119. }
  120. public override long? RecordId {
  121. get {
  122. PrepareSystemData();
  123. return (long?)this.systemProperties.RecordId;
  124. }
  125. }
  126. public override string ProviderName {
  127. get {
  128. PrepareSystemData();
  129. return this.systemProperties.ProviderName;
  130. }
  131. }
  132. public override Guid? ProviderId {
  133. get {
  134. PrepareSystemData();
  135. return this.systemProperties.ProviderId;
  136. }
  137. }
  138. public override string LogName {
  139. get {
  140. PrepareSystemData();
  141. return this.systemProperties.ChannelName;
  142. }
  143. }
  144. public override int? ProcessId {
  145. get {
  146. PrepareSystemData();
  147. return (int?)this.systemProperties.ProcessId;
  148. }
  149. }
  150. public override int? ThreadId {
  151. get {
  152. PrepareSystemData();
  153. return (int?)this.systemProperties.ThreadId;
  154. }
  155. }
  156. public override string MachineName {
  157. get {
  158. PrepareSystemData();
  159. return this.systemProperties.ComputerName;
  160. }
  161. }
  162. public override System.Security.Principal.SecurityIdentifier UserId {
  163. get {
  164. PrepareSystemData();
  165. return this.systemProperties.UserId;
  166. }
  167. }
  168. public override DateTime? TimeCreated {
  169. get {
  170. PrepareSystemData();
  171. return this.systemProperties.TimeCreated;
  172. }
  173. }
  174. public override Guid? ActivityId {
  175. get {
  176. PrepareSystemData();
  177. return this.systemProperties.ActivityId;
  178. }
  179. }
  180. public override Guid? RelatedActivityId {
  181. get {
  182. PrepareSystemData();
  183. return this.systemProperties.RelatedActivityId;
  184. }
  185. }
  186. public string ContainerLog {
  187. get {
  188. if (this.containerChannel != null)
  189. return this.containerChannel;
  190. lock (this.syncObject) {
  191. if (this.containerChannel == null) {
  192. this.containerChannel = (string)NativeWrapper.EvtGetEventInfo(this.Handle, UnsafeNativeMethods.EvtEventPropertyId.EvtEventPath);
  193. }
  194. return this.containerChannel;
  195. }
  196. }
  197. }
  198. public IEnumerable<int> MatchedQueryIds {
  199. get {
  200. if (this.matchedQueryIds != null)
  201. return this.matchedQueryIds;
  202. lock (this.syncObject) {
  203. if (this.matchedQueryIds == null) {
  204. this.matchedQueryIds = (int[])NativeWrapper.EvtGetEventInfo(this.Handle, UnsafeNativeMethods.EvtEventPropertyId.EvtEventQueryIDs);
  205. }
  206. return this.matchedQueryIds;
  207. }
  208. }
  209. }
  210. public override EventBookmark Bookmark {
  211. [System.Security.SecuritySafeCritical]
  212. get {
  213. EventLogPermissionHolder.GetEventLogPermission().Demand();
  214. EventLogHandle bookmarkHandle = NativeWrapper.EvtCreateBookmark(null);
  215. NativeWrapper.EvtUpdateBookmark(bookmarkHandle, this.handle);
  216. string bookmarkText = NativeWrapper.EvtRenderBookmark(bookmarkHandle);
  217. return new EventBookmark(bookmarkText);
  218. }
  219. }
  220. public override string FormatDescription() {
  221. return this.cachedMetadataInformation.GetFormatDescription(this.ProviderName, this.handle);
  222. }
  223. public override string FormatDescription(IEnumerable<object> values) {
  224. if (values == null) return this.FormatDescription();
  225. //copy the value IEnumerable to an array.
  226. string[] theValues = new string[0];
  227. int i = 0;
  228. foreach (object o in values) {
  229. if ( theValues.Length == i )
  230. Array.Resize( ref theValues, i+1 );
  231. theValues[i] = o.ToString();
  232. i++;
  233. }
  234. return this.cachedMetadataInformation.GetFormatDescription(this.ProviderName, this.handle, theValues);
  235. }
  236. public override string LevelDisplayName {
  237. get {
  238. if ( this.levelNameReady )
  239. return this.levelName;
  240. lock (this.syncObject) {
  241. if (this.levelNameReady == false) {
  242. this.levelNameReady = true;
  243. this.levelName = this.cachedMetadataInformation.GetLevelDisplayName(this.ProviderName, this.handle);
  244. }
  245. return this.levelName;
  246. }
  247. }
  248. }
  249. public override string OpcodeDisplayName {
  250. get {
  251. lock (this.syncObject) {
  252. if (this.opcodeNameReady == false) {
  253. this.opcodeNameReady = true;
  254. this.opcodeName = this.cachedMetadataInformation.GetOpcodeDisplayName(this.ProviderName, this.handle);
  255. }
  256. return this.opcodeName;
  257. }
  258. }
  259. }
  260. public override string TaskDisplayName {
  261. get {
  262. if (this.taskNameReady == true)
  263. return this.taskName;
  264. lock (this.syncObject) {
  265. if (this.taskNameReady == false) {
  266. this.taskNameReady = true;
  267. this.taskName = this.cachedMetadataInformation.GetTaskDisplayName(this.ProviderName, this.handle);
  268. }
  269. return this.taskName;
  270. }
  271. }
  272. }
  273. public override IEnumerable<string> KeywordsDisplayNames {
  274. get {
  275. if (this.keywordsNames != null)
  276. return this.keywordsNames;
  277. lock (this.syncObject) {
  278. if (this.keywordsNames == null) {
  279. this.keywordsNames = this.cachedMetadataInformation.GetKeywordDisplayNames(this.ProviderName, this.handle);
  280. }
  281. return this.keywordsNames;
  282. }
  283. }
  284. }
  285. public override IList<EventProperty> Properties {
  286. get {
  287. this.session.SetupUserContext();
  288. IList<object> properties = NativeWrapper.EvtRenderBufferWithContextUserOrValues(this.session.renderContextHandleUser, this.handle);
  289. List<EventProperty> list = new List<EventProperty>();
  290. foreach (object value in properties) {
  291. list.Add(new EventProperty(value));
  292. }
  293. return list;
  294. }
  295. }
  296. public IList<object> GetPropertyValues(EventLogPropertySelector propertySelector) {
  297. if (propertySelector == null)
  298. throw new ArgumentNullException("propertySelector");
  299. return NativeWrapper.EvtRenderBufferWithContextUserOrValues(propertySelector.Handle, this.handle);
  300. }
  301. // marked as SecurityCritical because it allocates SafeHandle
  302. // marked as TreatAsSafe because it performs Demand.
  303. [System.Security.SecuritySafeCritical]
  304. public override string ToXml() {
  305. EventLogPermissionHolder.GetEventLogPermission().Demand();
  306. StringBuilder renderBuffer = new StringBuilder(2000);
  307. NativeWrapper.EvtRender(EventLogHandle.Zero, this.handle, UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventXml, renderBuffer);
  308. return renderBuffer.ToString();
  309. }
  310. [System.Security.SecuritySafeCritical]
  311. protected override void Dispose(bool disposing) {
  312. try {
  313. if (disposing) {
  314. EventLogPermissionHolder.GetEventLogPermission().Demand();
  315. }
  316. if ( this.handle != null && !this.handle.IsInvalid )
  317. this.handle.Dispose();
  318. }
  319. finally {
  320. base.Dispose(disposing);
  321. }
  322. }
  323. // marked as SecurityCritical because allocates SafeHandle.
  324. [System.Security.SecurityCritical]
  325. internal static EventLogHandle GetBookmarkHandleFromBookmark(EventBookmark bookmark) {
  326. if (bookmark == null)
  327. return EventLogHandle.Zero;
  328. EventLogHandle handle = NativeWrapper.EvtCreateBookmark(bookmark.BookmarkText);
  329. return handle;
  330. }
  331. }
  332. }