private static void InitializeLogging() { const string TEMPORARY_DIRECTORY_NAME = "Temp"; const string DEBUG_LOG_NAME = "DebugLog.txt"; const int MAX_LOG_LENGTH_BYTES = 1 * 1024 * 1024; const int MAX_BACKUP_FILES = 10; string temporaryDir = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), TEMPORARY_DIRECTORY_NAME); string logFileName = Path.Combine(temporaryDir, DEBUG_LOG_NAME); // Create the directory if it doesn't already exist. if (!Directory.Exists(temporaryDir)) { Directory.CreateDirectory(temporaryDir); } // Setup a trace listener to our log file! var fs = new FileStreamWithBackup(logFileName, MAX_LOG_LENGTH_BYTES, MAX_BACKUP_FILES, FileMode.Append); fs.CanSplitData = false; var listener = new TextWriterTraceListenerWithTime(fs); Trace.Listeners.Add(listener); }
internal static void SetupTraceListners() { if (!Registry.Instance.CoreConfig.Trace.Enabled) { return; } //CustomTraceEnabled is enabled, lets try to instantiate it TextWriterTraceListenerWithTime traceListner = null; try { traceListner = new TextWriterTraceListenerWithTime(Registry.Instance.CoreConfig.Trace.File); traceListner.Filter = new EventTypeFilter(Registry.Instance.CoreConfig.Trace.Filter); traceListner.Attributes.Add("EmailErrors", Registry.Instance.CoreConfig.Trace.EmailError.ToString()); traceListner.Attributes.Add("SMTPServer", Registry.Instance.CoreConfig.Email.SmtpServer); traceListner.Attributes.Add("FromEmailId", Registry.Instance.CoreConfig.Email.FromEmail); traceListner.Attributes.Add("ToEmailIds", Registry.Instance.CoreConfig.Email.ToEmails); Trace.Listeners.Add(traceListner); Trace.WriteLine(string.Format("Trying to initialize {0} type trace listner.", traceListner.GetType().ToString())); Trace.Flush(); //if we are here, we are good, lets remove Default 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); if (!EventLog.SourceExists(Information.EventLogSource)) { EventLog.CreateEventSource(Information.EventLogSource, Information.EventLogName); } EventLog.WriteEntry(Information.EventLogSource, errMsg, EventLogEntryType.Error); } }