/// <summary> /// Outputs message to console. /// </summary> /// <param name="message">Text of message to output.</param> /// <param name="messageType">Message Level: Information, Warning, Error, Alert.</param> /// <param name="writeToAppLog">Copy messaage to the app log.</param> /// <param name="writeToEventLog">Copy message to the windows application event log.</param> public static void DisplayMessage(string message, pfConsoleMessageType messageType, bool writeToAppLog, bool writeToEventLog) { string messagePrefix = string.Empty; if (messageType != pfConsoleMessageType.Information) { messagePrefix = messageType.ToString() + ": "; } try { if (writeToAppLog) { WriteToAppLog(message, messageType); } if (writeToEventLog) { EventLogEntryType eventLogMessageType; switch (messageType) { case pfConsoleMessageType.Error: eventLogMessageType = EventLogEntryType.Error; break; case pfConsoleMessageType.Warning: eventLogMessageType = EventLogEntryType.Warning; break; default: eventLogMessageType = EventLogEntryType.Information; break; } WriteToEventLog(message, eventLogMessageType); } Console.WriteLine(messagePrefix + message); } catch (Exception ex) { Console.WriteLine("Unexpected Error in DisplayMessage" + ex.Source + "\r\n" + ex.Message + "\r\n" + ex.StackTrace); } }
/// <summary> /// Writes messeage to the app log file. /// </summary> /// <param name="message">Text of the message to output.</param> /// <param name="messageType">Use pfConsoleMessageType enumeration to specify type of message to output.</param> public static void WriteToAppLog(string message, pfConsoleMessageType messageType) { string appLogFilename = ""; string separator = new string('-', 50); string msgType = ""; try { if (_appLogFilename.Length > 0) { if (Path.GetDirectoryName(_appLogFilename).Length == 0) { appLogFilename = Environment.CurrentDirectory + @"\" + _appLogFilename; } else { appLogFilename = _appLogFilename; } } else { appLogFilename = Environment.CurrentDirectory + @"\" + _prodName + "_log.txt"; } StreamWriter log = new StreamWriter(appLogFilename, true); switch (messageType) { case pfConsoleMessageType.Error: msgType = "Error:\n"; break; case pfConsoleMessageType.Warning: msgType = "Warning:\n"; break; default: msgType = string.Empty; break; } if (_copyConsoleOutputToAppLog) { //console output likely being copied to app log to simulate redirecting of output //output redirection does not work when runas administrator processing being done by some apps if (messageType == pfConsoleMessageType.Warning || messageType == pfConsoleMessageType.Error) { log.WriteLine(messageType.ToString() + ": " + message); } else { log.WriteLine(message); } } else { log.WriteLine(separator); log.WriteLine(msgType + DateTime.Now.ToString()); log.WriteLine(separator); log.WriteLine(message); log.WriteLine(""); } log.Flush(); log.Close(); } catch (Exception ex) { string errorMessage = ex.Source + "\r\n" + ex.Message + "\r\n" + ex.StackTrace; Console.WriteLine(errorMessage); } }