Log.cs class

xiaoxiao2021-04-05  272

A class that handles information and, add information, error, and warning to the Event View class

Using system.id; using system.diagnostics; using system.configuration;

Namespace Common {///

/// summary description for logs. /// public class logs {/// /// 错 信息 信息 信息 加 加 加 加 加 方法 加 方法 方法> /// Error information Type: "e" indicates an error, "W" means warning, "i" means information /// Public Void Eventlog (String ErrorType, String ErrorMessage) {String Traceconfig; String EventLogname

EventLogname = "EventLogname"; System.Diagnostics.Eventlog ObjeventLog = New EventLog (); ObjeventLog.Source = "EventLogname";

IF (! system.diagnostics.eventlog.sourceexists (EventLogname)) {system.diagnostics.eventlog.createeventsource (EventLogname, EventLogname);

Try {// Value from the app.config file // If you are info, add any information // if Error, then ERROR load only, and warning information // If Warn, Only EVENT VIEW is loaded with only an error message traceconfig = configurationSettings.appsettings ["trace mode"];} catch {traceConfig = "info";}

Try {IF (traceConfig.toupper () == "info") {if (erroType == "e") {ObjeventLog.writeEntry (system.datetime.now.tostring () " errorMessage, EventLoGentryType.Error); WriteLogToFile (System.DateTime.Now.ToString () "" ErrorMessage);} else if (ErrorType == "W") {objEventLog.WriteEntry (System.DateTime.Now.ToString () "" ErrorMessage, EventLogEntryType .Warning);} else if (ErrorType == "I") {ObjeventLog.writeEntry () " ErrorMessage, EventLoGentrytype.information);}} // Info else IF (TraceConfig. ToUpper () == "WARNING") {if (ErrorType == "E") {objEventLog.WriteEntry (System.DateTime.Now.ToString () "" ErrorMessage, EventLogEntryType.Error); WriteLogToFile (System.DateTime. Now.toString () " ErrorMessage);} else if (ErrorTyPe ==" W ") {ObjeventLog.writeEntry (System.Datetime.now.toString () " ErrorMessage, Eventlogentr Ytype.warning);}} // Warning

else if (TraceConfig.ToUpper () == "ERROR") {if (ErrorType == "E") {objEventLog.WriteEntry (System.DateTime.Now.ToString () "" ErrorMessage, EventLogEntryType.Error); WriteLogToFile (System.datetime.now.tostring () " errorMessage);}} // error

Else {Objeventlog.writentry (System.Datetime.now.Tostring () " ErrorMessage, EventLoGentrytype.Error);} // try}} ///

/// will log log Write file /// /// private void writelogtofile (string errorMessage) {Try {string logfilepath = "c: // Documents and settings"; ///// ConfigurationSettings.AppSettingS [""]; // -------- app.config Fildes unknow string machinename = Environment.usename.toString (); // configurationSettings.appsettings [""]; // -------- ---- App.config Fildes Unknow String ApplicationID = "Desktop"; // ConfigurationSettings.AppSettings [""]; // -------- App.config Fildes Unknow

String currentpath = logfilepath; DirectoryInfo Di = New DirectoryInfo (CURRENTPATH);

IF (di.exists == true) {currentpath = currentpath "//" Machinename; di = new DirectoryInfo (CurrentPath); if (di.exists == true) {// do nothing} else {di.create }

CurrentPath = CURRENTPATH ​​ "//" ApplicationID; Di = New DirectoryInfo (CURRENTPATH); if (di.exists == true) {// do nothing} else {di.create ();}

CurrentPath = CURRENTPATH ​​ "//" "Error.txt"; streamwriter stw = new streamwriter (currentpath, false); stw.writeline (system.datetime.now.tostring () " errorMessage); stw.close );}} Catch {// do nothing}}

} // Class logs

}

转载请注明原文地址:https://www.9cbs.com/read-132002.html

New Post(0)