C # log recording,
/// <Summary> /// log Part /// </summary> /// <param name = "fileName"> </param> /// <param name = "type"> </param> // <param name = "content"> </param> public static void WriteLogs (string fileName, string type, string content) {string path = AppDomain. currentDomain. baseDirectory; if (! String. IsNullOrEmpty (path) {path = AppDomain. CurrentDomain. BaseDirectory + fileName; if (! Directory. Exists (path) {Directory. CreateDirectory (path);} path = path + "\" + DateTime. Now. ToString ("yyyyMMdd"); if (! Directory. exists (path) {Directory. createDirectory (path);} path = path + "\" + DateTime. now. toString ("yyyyMMdd") + ". txt "; if (! File. exists (path) {FileStream fs = File. create (path); fs. close ();} if (File. exists (path) {StreamWriter sw = new StreamWriter (path, true, System. text. encoding. default); sw. writeLine (DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fff ") + type +" --> "+ content); // sw. writeLine ("--------------------------------------"); sw. close ();}}}
Using System; using System. collections. generic; using System. text; using System. diagnostics; namespace Utils {// <summary> /// <para> </para> // common tool class -- System Log class // <para> usage </para>/ // <para> WriteEventLog: write System logs (reload using two methods) </para> // <para> DelEventName: delete log event source category </para> /// </summary> public class EventLogHelper {# Write region to system logs /// <summary> /// write to system logs/ /// </Summary> /// <param name = "EventName"> event source name </param> /// <param name = "LogStr"> log content </param> public static void WriteEventLog (string EventName, string LogStr) {try {if (! EventLog. sourceExists (EventName) {EventLog. createEventSource (EventName, EventName);} EventLog. writeEntry (EventName, LogStr);} catch (Exception) {}}/// <summary> /// Write System logs /// </summary> /// <param name = "EventName"> event source name </param> /// <param name = "LogType"> Log Type </param> /// <param name = "LogStr"> log content </param> public static void WriteEventLog (string eventName, string LogStr, EventLogEntryType LogT Ype) {try {if (! EventLog. sourceExists (EventName) {EventLog. createEventSource (EventName, EventName);} EventLog. writeEntry (EventName, LogStr, LogType);} catch (Exception) {}}# endregion # region deletes the log event source category /// <summary> /// deletes the log event source category /// </summary> /// <param name = "EventName"> event source name </param> // <returns> </returns> public static bool DelEventName (string EventName) {bool flag = false; try {if (EventLog. sourceExists (EventName) {EventLog. deleteEventSource (EventName ,". "); flag = true ;}} catch (Exception) {} return flag ;}# endregion }}
EventLogHelper