示例#1
0
文件: SetupTrace.cs 项目: eyedia/idpe
        public static void SetupTraceListners(string eventLogSource, string eventLogName)
        {
            if (!EyediaCoreConfigurationSection.CurrentConfig.Trace.Enabled)
            {
                return;
            }

            TextWriterTraceListenerWithTime traceListner = null;

            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(EyediaCoreConfigurationSection.CurrentConfig.Trace.File)))
                {
                    throw new DirectoryNotFoundException(Path.GetDirectoryName(EyediaCoreConfigurationSection.CurrentConfig.Trace.File) + " not found! ");
                }

                FileStreamWithBackup fileStreamWithBackup = new FileStreamWithBackup(EyediaCoreConfigurationSection.CurrentConfig.Trace.File, 5 * 1024 * 1024, EyediaCoreConfigurationSection.CurrentConfig.Trace.MaxFileCount, FileMode.Append);
                traceListner        = new TextWriterTraceListenerWithTime(EyediaCoreConfigurationSection.CurrentConfig.Trace.File, EyediaCoreConfigurationSection.CurrentConfig.Trace.MaxFileCount, fileStreamWithBackup);
                traceListner.Filter = new EventTypeFilter(EyediaCoreConfigurationSection.CurrentConfig.Trace.Filter);
                traceListner.Attributes.Add("EmailErrors", EyediaCoreConfigurationSection.CurrentConfig.Trace.EmailError.ToString());
                traceListner.Attributes.Add("SMTPServer", EyediaCoreConfigurationSection.CurrentConfig.Email.SmtpServer);
                traceListner.Attributes.Add("FromEmailId", EyediaCoreConfigurationSection.CurrentConfig.Email.FromEmail);
                traceListner.Attributes.Add("ToEmailIds", EyediaCoreConfigurationSection.CurrentConfig.Email.ToEmails);

                Trace.Listeners.Clear();
                Trace.Listeners.Add(traceListner);
                Trace.Listeners.Remove("Default");
            }
            catch (Exception ex)
            {
                string errMsg = "Could not instantiate TextWriterTraceListenerWithTime type listner. Check configuration, file path & permission."
                                + ex.ToString() + (ex.InnerException == null ? string.Empty : ex.InnerException.Message);

                try
                {
                    if (!EventLog.SourceExists(eventLogSource))
                    {
                        EventLog.CreateEventSource(eventLogSource, eventLogName);
                    }
                    EventLog.WriteEntry(eventLogSource, errMsg, EventLogEntryType.Error);
                }
                catch
                {
                    throw new Exception("You are running the application for very first time on this machine. The event log source could not be created because access was denied. Please run the application as administrator!");
                }
            }
        }
示例#2
0
文件: SetupTrace.cs 项目: eyedia/idpe
        public static void Clear(string eventLogSource, string eventLogName)
        {
            try
            {
                if (Trace.Listeners[0] is TextWriterTraceListenerWithTime)
                {
                    TextWriterTraceListenerWithTime listener = Trace.Listeners[0] as TextWriterTraceListenerWithTime;
                    listener.FileStream.BackupAndResetStream();
                }
            }
            catch (Exception ex)
            {
                string errMsg = "Could not instantiate TextWriterTraceListenerWithTime type listner. Check configuration, file path & permission."
                                + ex.ToString() + (ex.InnerException == null ? string.Empty : ex.InnerException.Message);

                if (!EventLog.SourceExists(eventLogSource))
                {
                    EventLog.CreateEventSource(eventLogSource, eventLogName);
                }
                EventLog.WriteEntry(eventLogSource, errMsg, EventLogEntryType.Error);
            }
        }