Ejemplo n.º 1
0
        private LogManager()
        {
            LogSection customSection = ConfigurationManager.GetSection("LogSection") as LogSection;
            if (customSection == null)
                customSection = new LogSection();

            try
            {
                this.excludeFlags = customSection.Exclude;

                string source = customSection.SourceName;
                string logName = customSection.LogName;

                myEventLog = new EventLog();
                myEventLog.Log = logName;
                myEventLog.Source = source;

                if( EventLog.SourceExists( source ) )
                    EventLog.DeleteEventSource( source );

                if( !System.Diagnostics.EventLog.SourceExists( source ) )
                    System.Diagnostics.EventLog.CreateEventSource( source, logName );

                if( myEventLog.OverflowAction != OverflowAction.OverwriteAsNeeded )
                    myEventLog.ModifyOverflowPolicy( OverflowAction.OverwriteAsNeeded, 0 );
            }
            catch( Exception ex )
            {
                Trace.TraceError( ex.Message + " at " + ex.Source );
            }
        }
Ejemplo n.º 2
0
        public static EventLog CreateLog(string logSource, string logName)
        {
            try
            {
                if (!EventLog.Exists(logName))
                {
                    EventLog.CreateEventSource(logSource, logName);
                }

                EventLog eventLog = new EventLog(logName);
                eventLog.Source = logSource;
                try
                {
                    eventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 7);
                }
                catch (Exception ex)
                {
                    OnModifyOverFlowException(ex);
                }

                return eventLog;
            }
            catch (Exception ex)
            {
                OnCreateLogException(ex);
                return null;
            }
        }
Ejemplo n.º 3
0
        static Logger()
        {
            TraceSwitch ts = new TraceSwitch("TraceLevelSwitch", "Determines the tracing level to log/display");
            TraceLevel = ts.Level;

            if (!EventLog.Exists("CSGO"))
            {
                EventLog.CreateEventSource(LOG_SOURCE, LOG_NAME);
            }

            try
            {
                _eventLog = new EventLog();
                _eventLog.Source = LOG_SOURCE;
                _eventLog.Log = LOG_NAME;
            }
            catch { }
            try
            {
                _eventLog.MaximumKilobytes = 200 * 1024;
                _eventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
            }
            catch
            {
            }
            Trace.Listeners.Clear();
            Trace.Listeners.Add(new EventLogTraceListener(_eventLog)); // writes to EventLog
            Trace.Listeners.Add(new ConsoleTraceListener(true)); // writes to Console window
            Trace.Listeners.Add(new DefaultTraceListener()); // writes to Output window
        }
Ejemplo n.º 4
0
        private static EventLog GetLog(string logSource, string logName)
        {
            if (!EventLog.Exists(logName))
            {
                EventLog.CreateEventSource(logSource, logName);
            }

            EventLog eventLog = new EventLog(logName);
            eventLog.Source = logSource;
            eventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 7);

            return eventLog;
        }
Ejemplo n.º 5
0
 public static void ModifyOverflowPolicy(string logSource, string logName, OverflowAction action, int retentionDays)
 {
     try
     {
         EventLog eventLog = new EventLog(logName);
         eventLog.Source = logSource;
         eventLog.ModifyOverflowPolicy(action, retentionDays);
     }
     catch (Exception ex)
     {
         OnModifyOverFlowException(ex);
     }
 }
Ejemplo n.º 6
0
 public void WriteEventLog(EventLogEntryType logType, string desc)
 {
     //只在错误级别大于配置文件设定的级别才允许记录日志
     if (int.Parse(logType.ToString("D")) <= int.Parse(traceLevel.ToString("D")))
     {
         if (!EventLog.Exists(eventLogName))
         {
             EventLog.CreateEventSource(eventLogName, eventLogName);
         }
         System.Diagnostics.EventLog log = new System.Diagnostics.EventLog(eventLogName, ".", eventLogName);
         log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 7);
         log.WriteEntry(desc, logType);
     }
 }
Ejemplo n.º 7
0
        private void ConfigureEventLog(System.Diagnostics.EventLog el)
        {
            if (this.MaxSize > 0)
            {
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting EventLog Size: {0}Mb", this.MaxSize));
                el.MaximumKilobytes = this.MaxSize * 1024;
            }

            if (this.Retention > 0)
            {
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Retention: {0} days", this.Retention));
                el.ModifyOverflowPolicy(OverflowAction.OverwriteOlder, this.Retention);
            }
            else if (this.Retention == -1)
            {
                this.LogTaskMessage("Setting Retention to 'Overwrite As Needed'");
                el.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
            }
            else if (this.Retention == -2)
            {
                this.LogTaskMessage("Setting Retention to 'Do Not Overwrite'");
                el.ModifyOverflowPolicy(OverflowAction.DoNotOverwrite, 0);
            }
        }
Ejemplo n.º 8
0
        protected ReportBuilderManager()
        {
            myEventLog = new EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("ReportProcessor"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                   "ReportProcessor", "ReportBuilder");
            }
            myEventLog.Source = "ReportProcessor";
            myEventLog.Log = "ReportBuilder";

            if (myEventLog.OverflowAction != OverflowAction.OverwriteAsNeeded)
            {
                myEventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Метод инициализации журнала событий Windows.
 /// </summary>
 public static void Init()
 {
     // Иницируем журнал событий Windows
     try
     {
         // Если не существует windows лога службы, то создаем его и назначаем по-умолчанию для записи
         if (!EventLog.SourceExists("SmartHouseService"))
             EventLog.CreateEventSource("SmartHouseService", "SmartHouseServiceLog");
         // Иницируем журнал событий Windows
         _log = new EventLog();
         _log.Source = "SmartHouseService";
         _log.Log = "SmartHouseServiceLog";
         _log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 30);
     }
     catch (Exception)
     {
         if (_log != null)
             _log.Dispose();
         _log = null;
     }
 }
Ejemplo n.º 10
0
 private static void SetupEventLogTraceListener()
 {
     if (!EventLog.SourceExists(eventLogSource))
     {
         EventLog.CreateEventSource(eventLogSource, eventLogName);
     }
     myEventLog = new EventLog();
     myEventLog.Source = eventLogSource;
     myEventLogTraceListener = new EventLogTraceListener(myEventLog);
     if (myEventLog.OverflowAction != OverflowAction.OverwriteAsNeeded)
     {
         myEventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 1);
     }
     Trace.Listeners.Add(myEventLogTraceListener);
 }
Ejemplo n.º 11
0
		protected override void BeginProcessing()
		{
			string str;
			string[] strArrays = this._computerName;
			for (int i = 0; i < (int)strArrays.Length; i++)
			{
				string str1 = strArrays[i];
				if (str1.Equals("localhost", StringComparison.CurrentCultureIgnoreCase) || str1.Equals(".", StringComparison.OrdinalIgnoreCase))
				{
					str = "localhost";
				}
				else
				{
					str = str1;
				}
				string[] strArrays1 = this._logName;
				for (int j = 0; j < (int)strArrays1.Length; j++)
				{
					string str2 = strArrays1[j];
					try
					{
						if (EventLog.Exists(str2, str1))
						{
							if (base.ShouldProcess(StringUtil.Format(EventlogResources.LimitEventLogWarning, str2, str)))
							{
								EventLog eventLog = new EventLog(str2, str1);
								int minimumRetentionDays = eventLog.MinimumRetentionDays;
								OverflowAction overflowAction = eventLog.OverflowAction;
								if (!this.retentionSpecified || !this.overflowSpecified)
								{
									if (!this.retentionSpecified || this.overflowSpecified)
									{
										if (!this.retentionSpecified && this.overflowSpecified)
										{
											eventLog.ModifyOverflowPolicy(this._overflowaction, minimumRetentionDays);
										}
									}
									else
									{
										if (overflowAction.CompareTo(OverflowAction.OverwriteOlder) != 0)
										{
											ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.InvalidOverflowAction, new object[0])), null, ErrorCategory.InvalidOperation, null);
											base.WriteError(errorRecord);
											goto Label0;
										}
										else
										{
											eventLog.ModifyOverflowPolicy(overflowAction, this._retention);
										}
									}
								}
								else
								{
									if (this._overflowaction.CompareTo(OverflowAction.OverwriteOlder) != 0)
									{
										ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.InvalidOverflowAction, new object[0])), null, ErrorCategory.InvalidOperation, null);
										base.WriteError(errorRecord1);
										goto Label0;
									}
									else
									{
										eventLog.ModifyOverflowPolicy(this._overflowaction, this._retention);
									}
								}
								if (this.maxkbSpecified)
								{
									int num = 0x400;
									this._maximumKilobytes = this._maximumKilobytes / (long)num;
									eventLog.MaximumKilobytes = this._maximumKilobytes;
								}
							}
						}
						else
						{
							ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.LogDoesNotExist, str2, str)), null, ErrorCategory.InvalidOperation, null);
							base.WriteError(errorRecord2);
						}
					}
					catch (InvalidOperationException invalidOperationException1)
					{
						InvalidOperationException invalidOperationException = invalidOperationException1;
						this.WriteNonTerminatingError(invalidOperationException, EventlogResources.PermissionDenied, "PermissionDenied", ErrorCategory.PermissionDenied, str2, str);
					}
					catch (IOException oException1)
					{
						IOException oException = oException1;
						this.WriteNonTerminatingError(oException, EventlogResources.PathDoesNotExist, "PathDoesNotExist", ErrorCategory.InvalidOperation, null, str);
					}
					catch (ArgumentOutOfRangeException argumentOutOfRangeException1)
					{
						ArgumentOutOfRangeException argumentOutOfRangeException = argumentOutOfRangeException1;
						if (this.retentionSpecified || this.maxkbSpecified)
						{
							this.WriteNonTerminatingError(argumentOutOfRangeException, EventlogResources.ValueOutofRange, "ValueOutofRange", ErrorCategory.InvalidData, null, null);
						}
						else
						{
							this.WriteNonTerminatingError(argumentOutOfRangeException, EventlogResources.InvalidArgument, "InvalidArgument", ErrorCategory.InvalidData, null, null);
						}
					}
                Label0:
                    continue;
				}
			}
		}
Ejemplo n.º 12
0
 BeginProcessing()
 {
     string computer = string.Empty;
     foreach (string compname in ComputerName)
     {
         if ((compname.Equals("localhost", StringComparison.CurrentCultureIgnoreCase)) || (compname.Equals(".", StringComparison.OrdinalIgnoreCase)))
         {
             computer = "localhost";
         }
         else
         {
             computer = compname;
         }
         foreach (string logname in LogName)
         {
             try
             {
                 if (!EventLog.Exists(logname, compname))
                 {
                     ErrorRecord er = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.LogDoesNotExist, logname, computer)), null, ErrorCategory.InvalidOperation, null);
                     WriteError(er);
                     continue;
                 }
                 else
                 {
                     if (!ShouldProcess(StringUtil.Format(EventlogResources.LimitEventLogWarning, logname, computer)))
                     {
                         continue;
                     }
                     else
                     {
                         EventLog newLog = new EventLog(logname, compname);
                         int _minRetention = newLog.MinimumRetentionDays;
                         System.Diagnostics.OverflowAction _newFlowAction = newLog.OverflowAction;
                         if (_retentionSpecified && _overflowSpecified)
                         {
                             if (_overflowaction.CompareTo(System.Diagnostics.OverflowAction.OverwriteOlder) == 0)
                             {
                                 newLog.ModifyOverflowPolicy(_overflowaction, _retention);
                             }
                             else
                             {
                                 ErrorRecord er = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.InvalidOverflowAction)), null, ErrorCategory.InvalidOperation, null);
                                 WriteError(er);
                                 continue;
                             }
                         }
                         else if (_retentionSpecified && !_overflowSpecified)
                         {
                             if (_newFlowAction.CompareTo(System.Diagnostics.OverflowAction.OverwriteOlder) == 0)
                             {
                                 newLog.ModifyOverflowPolicy(_newFlowAction, _retention);
                             }
                             else
                             {
                                 ErrorRecord er = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.InvalidOverflowAction)), null, ErrorCategory.InvalidOperation, null);
                                 WriteError(er);
                                 continue;
                             }
                         }
                         else if (!_retentionSpecified && _overflowSpecified)
                         {
                             newLog.ModifyOverflowPolicy(_overflowaction, _minRetention);
                         }
                         if (_maxkbSpecified)
                         {
                             int kiloByte = 1024;
                             _maximumKilobytes = _maximumKilobytes / kiloByte;
                             newLog.MaximumKilobytes = _maximumKilobytes;
                         }
                     }
                 }
             }
             catch (InvalidOperationException ex)
             {
                 WriteNonTerminatingError(ex, EventlogResources.PermissionDenied, "PermissionDenied", ErrorCategory.PermissionDenied, logname, computer);
                 continue;
             }
             catch (System.IO.IOException ex)
             {
                 WriteNonTerminatingError(ex, EventlogResources.PathDoesNotExist, "PathDoesNotExist", ErrorCategory.InvalidOperation, null, computer);
                 continue;
             }
             catch (ArgumentOutOfRangeException ex)
             {
                 if (!_retentionSpecified && !_maxkbSpecified)
                 {
                     WriteNonTerminatingError(ex, EventlogResources.InvalidArgument, "InvalidArgument", ErrorCategory.InvalidData, null, null);
                 }
                 else
                 {
                     WriteNonTerminatingError(ex, EventlogResources.ValueOutofRange, "ValueOutofRange", ErrorCategory.InvalidData, null, null);
                 }
                 continue;
             }
         }
     }
 }
Ejemplo n.º 13
0
 static void SetOptionsIfNeed(EventLog log)
 {
     if (log.OverflowAction != OverflowAction.OverwriteAsNeeded)
     {
         log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 1);
         log.MaximumKilobytes = 512000;
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Creates a new AucentCustomEventLogSource.  Pass in your TaxonomyFileName (without extension) to create your log.
        /// </summary>
        public RivetCustomEventLogSource(string EventLogTitle, string EventSourceName, string TargetMachineName)
        {
            try
            {
                if (TargetMachineName == string.Empty || TargetMachineName == null)
                    TargetMachineName = Environment.MachineName;

                eventLogName = EventLogTitle;

                if(EventLog.SourceExists(EventSourceName, TargetMachineName))
                    // Are we dealing with the same log for the source?
                    if (EventLogTitle.Trim() != EventLog.LogNameFromSourceName(EventSourceName, TargetMachineName).Trim())
                        EventLog.DeleteEventSource(EventSourceName, TargetMachineName);

                // Create EventLog if needed
                if(!EventLog.Exists(EventLogTitle, TargetMachineName))
                {
                    if (EventLog.SourceExists(EventLogTitle, TargetMachineName))
                    {
                        // If an event source exists in the name of the EventLog, we should remove the
                        // event source
                        try
                        {
                            EventLog.DeleteEventSource(EventLogTitle, TargetMachineName);
                        }
                        catch
                        {
                            // If we had an issue with deleting the EventSource because it matches some EventLog title,
                            // we have no choice but to remove the EventLog alltogether!
                            EventLog.Delete(EventLogTitle, TargetMachineName);
                        }
                    }
                    EventSourceCreationData dataSource = new EventSourceCreationData(EventSourceName, EventLogTitle);
                    dataSource.MachineName = TargetMachineName;
                    EventLog.CreateEventSource(dataSource);
                }
                // Create the event source if it does not exist
                if (!EventLog.SourceExists(EventSourceName, TargetMachineName))
                {
                    EventSourceCreationData dataSource = new EventSourceCreationData(EventSourceName, EventLogTitle);
                    dataSource.MachineName = TargetMachineName;
                    EventLog.CreateEventSource(dataSource);
                }
                if (!eventLogsUpdated.Contains(EventLogTitle) )
                {
                    EventLog myLog = new EventLog(EventLogTitle, TargetMachineName);
                    if (myLog.OverflowAction != OverflowAction.OverwriteAsNeeded)
                    {
                        myLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
                    }

                    eventLogsUpdated.Add(EventLogTitle);
                }

            }
            catch(System.Exception ex)
            {
                string msg = ex.Message;
            }
        }
Ejemplo n.º 15
0
		public void Create()
		{
			string builtInSourceName = string.Concat(_logName, "Instrumentation");
			bool sourcesContainedBuildIn = false;

			// create sources
			foreach (string source in _sources)
			{
				CreateSource(source);
				if (source == builtInSourceName)
					sourcesContainedBuildIn = true;
			}

			if (!sourcesContainedBuildIn)
			{
				CreateSource(builtInSourceName);
			}

			using (EventLog eventLog = new EventLog(_logName, _machineName, builtInSourceName))
			{
				eventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 14);
				eventLog.MaximumKilobytes = _logSizeInMegaBytes * 1024;
				eventLog.WriteEntry("Log created.", EventLogEntryType.Information);
				eventLog.Dispose();
			}
		}
Ejemplo n.º 16
0
        private void InitWindowsEventLog()
        {
            if (this._windowsLogInitialized)
                return;

            if (!InitWindowsEventSource(Const.DEFAULT_APPLICATION_NAME, Const.WINDOWS_LOG_NAME))
                return;

            // Modification des paramètres du journal d'évènement
            if (EventLog.Exists(Const.WINDOWS_LOG_NAME))
            {
                EventLog a = new EventLog(Const.WINDOWS_LOG_NAME);
                if (a.OverflowAction != OverflowAction.OverwriteAsNeeded)
                {
                    a.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 7);
                }

                if (a.MaximumKilobytes < 2048)
                {
                    a.MaximumKilobytes = 2048;
                }
            }

            this._windowsLogInitialized = true;
        }
Ejemplo n.º 17
0
 // ------- Инициализация --------
 protected void InitLog()
 {
     if (!EventLog.Exists(myLog) || !EventLog.SourceExists(mySource))
     {
         EventLog.CreateEventSource(mySource, myLog);
         EventLog log = new EventLog(myLog);
         log.MaximumKilobytes = maximumKilobytes;
         log.ModifyOverflowPolicy(OverflowAction.OverwriteOlder, retentionDay);
     }
 }