1 // ==++== 2 // 3 // Copyright (c) Microsoft Corporation. All rights reserved. 4 // 5 // ==--== 6 /*============================================================ 7 ** 8 ** Class: EventLogConfiguration 9 ** 10 ** Purpose: 11 ** This public class allows accessing static channel information and 12 ** configures channel publishing and logging properties. An instance 13 ** of this class is obtained from EventLogManagement class. 14 ** 15 ============================================================*/ 16 17 using System.Collections.Generic; 18 using System.Runtime.InteropServices; 19 using System.Security.Permissions; 20 using Microsoft.Win32; 21 22 namespace System.Diagnostics.Eventing.Reader { 23 24 /// <summary> 25 /// Log Type 26 /// </summary> 27 public enum EventLogType { 28 Administrative = 0, 29 Operational, 30 Analytical, 31 Debug 32 } 33 34 /// <summary> 35 /// Log Isolation 36 /// </summary> 37 public enum EventLogIsolation { 38 Application = 0, 39 System, 40 Custom 41 } 42 43 /// <summary> 44 /// Log Mode 45 /// </summary> 46 public enum EventLogMode { 47 Circular = 0, 48 AutoBackup, 49 Retain 50 } 51 52 /// <summary> 53 /// Provides access to static log information and configures 54 /// log publishing and log file properties. 55 /// </summary> 56 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 57 public class EventLogConfiguration : IDisposable { 58 59 // 60 // access to the data member reference is safe, while 61 // invoking methods on it is marked SecurityCritical as appropriate. 62 // 63 private EventLogHandle handle = EventLogHandle.Zero; 64 65 private EventLogSession session = null; 66 private string channelName; 67 EventLogConfiguration(string logName)68 public EventLogConfiguration(string logName) : this(logName, null) { } 69 70 // marked as SecurityCritical because allocates SafeHandles. 71 // marked as Safe because performs Demand check. 72 [System.Security.SecurityCritical] EventLogConfiguration(string logName, EventLogSession session)73 public EventLogConfiguration(string logName, EventLogSession session) { 74 75 EventLogPermissionHolder.GetEventLogPermission().Demand(); 76 77 if (session == null) 78 session = EventLogSession.GlobalSession; 79 80 this.session = session; 81 this.channelName = logName; 82 83 handle = NativeWrapper.EvtOpenChannelConfig(this.session.Handle, this.channelName, 0); 84 } 85 86 public string LogName { 87 get { 88 return channelName; 89 } 90 } 91 92 public EventLogType LogType { 93 get { 94 return (EventLogType)((uint)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigType)); 95 } 96 } 97 98 public EventLogIsolation LogIsolation { 99 get { 100 return (EventLogIsolation)((uint)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigIsolation)); 101 } 102 } 103 104 public bool IsEnabled { 105 get { 106 return (bool)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigEnabled); 107 } 108 set { 109 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigEnabled, (object)value); 110 } 111 } 112 113 public bool IsClassicLog { 114 get { 115 return (bool)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigClassicEventlog); 116 } 117 } 118 119 public string SecurityDescriptor { 120 get { 121 return (string)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigAccess); 122 } 123 set { 124 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigAccess, (object)value); 125 } 126 } 127 128 public string LogFilePath { 129 get { 130 return (string)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigLogFilePath); 131 } 132 set { 133 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigLogFilePath, (object)value); 134 } 135 } 136 137 public long MaximumSizeInBytes { 138 get { 139 return (long)((ulong)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigMaxSize)); 140 } 141 set { 142 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigMaxSize, (object)value); 143 } 144 } 145 146 public EventLogMode LogMode { 147 get { 148 object nativeRetentionObject = NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention); 149 object nativeAutoBackupObject = NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup); 150 151 bool nativeRetention = nativeRetentionObject == null ? false : (bool)nativeRetentionObject; 152 bool nativeAutoBackup = nativeAutoBackupObject == null ? false : (bool)nativeAutoBackupObject; 153 154 if (nativeAutoBackup) 155 return EventLogMode.AutoBackup; 156 157 if (nativeRetention) 158 return EventLogMode.Retain; 159 160 return EventLogMode.Circular; 161 } 162 set { 163 164 switch (value) { 165 case EventLogMode.Circular: 166 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup, (object)false); 167 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention, (object)false); 168 break; 169 case EventLogMode.AutoBackup: 170 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup, (object)true); 171 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention, (object)true); 172 break; 173 case EventLogMode.Retain: 174 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup, (object)false); 175 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention, (object)true); 176 break; 177 } 178 } 179 } 180 181 public string OwningProviderName { 182 get { 183 return (string)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigOwningPublisher); 184 } 185 } 186 187 public IEnumerable<string> ProviderNames { 188 get { 189 return (string[])NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublisherList); 190 } 191 } 192 193 public int? ProviderLevel { 194 get { 195 return (int?)((uint?)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigLevel)); 196 } 197 set { 198 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigLevel, (object)value); 199 } 200 } 201 202 public long? ProviderKeywords { 203 get { 204 return (long?)((ulong?)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigKeywords)); 205 } 206 set { 207 NativeWrapper.EvtSetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigKeywords, (object)value); 208 } 209 } 210 211 public int? ProviderBufferSize { 212 get { 213 return (int?)((uint?)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigBufferSize)); 214 } 215 } 216 217 public int? ProviderMinimumNumberOfBuffers { 218 get { 219 return (int?)((uint?)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigMinBuffers)); 220 } 221 } 222 223 public int? ProviderMaximumNumberOfBuffers { 224 get { 225 return (int?)((uint?)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigMaxBuffers)); 226 } 227 } 228 229 public int? ProviderLatency { 230 get { 231 return (int?)((uint?)NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigLatency)); 232 } 233 } 234 235 public Guid? ProviderControlGuid { 236 get { 237 return (Guid?)(NativeWrapper.EvtGetChannelConfigProperty(this.handle, UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigControlGuid)); 238 } 239 } 240 SaveChanges()241 public void SaveChanges() { 242 243 NativeWrapper.EvtSaveChannelConfig(this.handle, 0); 244 } 245 Dispose()246 public void Dispose() { 247 Dispose(true); 248 GC.SuppressFinalize(this); 249 } 250 251 252 [System.Security.SecuritySafeCritical] Dispose(bool disposing)253 protected virtual void Dispose(bool disposing) { 254 if (disposing) { 255 EventLogPermissionHolder.GetEventLogPermission().Demand(); 256 } 257 if ( handle != null && !handle.IsInvalid ) 258 handle.Dispose(); 259 } 260 } 261 } 262