// sends a log request via email. // actual email 'Send' calls are commented out. // uncomment if you have the proper email privileges. public void Log(object sender, LogEventArgs e) { string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message; // commented out for now. you need privileges to send email. // _smtpClient.Send(from, to, subject, body); }
public void Log(object sender, LogEventArgs e) { // example code of entering a log event to output console string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message; // writes message to debug output window System.Diagnostics.Debugger.Log(0, null, message + "\r\n\r\n"); }
// actual database insert statements are commented out. // you can activate this if you have the proper database // configuration and access privileges. public void Log(object sender, LogEventArgs e) { // example code of entering a log event to database string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message; // something like string sql = "INSERT INTO LOG (message) VALUES('" + message + "')"; // commented out for now. You need database to store log values. //Db.Update(sql); }
public void Log(object sender, LogEventArgs e) { string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message; var eventLog = new EventLog(); eventLog.Source = "Patterns In Action"; // map severity level to an Windows EventLog entry type var type = EventLogEntryType.Error; if (e.Severity < LogSeverity.Warning) type = EventLogEntryType.Information; if (e.Severity < LogSeverity.Error) type = EventLogEntryType.Warning; // in try catch. You will need proper privileges to write to eventlog try { eventLog.WriteEntry(message, type); } catch { /* do nothing */ } }
// write a log request to a file public void Log(object sender, LogEventArgs e) { string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message; FileStream fileStream; // create directory, if necessary try { fileStream = new FileStream(fileName, FileMode.Append); } catch (DirectoryNotFoundException) { Directory.CreateDirectory((new FileInfo(fileName)).DirectoryName); fileStream = new FileStream(fileName, FileMode.Append); } // NOTE: be sure you have write privileges to folder var writer = new StreamWriter(fileStream); try { writer.WriteLine(message); } catch { /* do nothing */} finally { try { writer.Close(); } catch { /* do nothing */} } }
// invokes the Log event public void OnLog(LogEventArgs e) { if (Log != null) { Log(this, e); } }