Int nFindSlashPos = strLogPath.Trim().LastIndexOf( " \\") / /// /// private static bool CheckDirectory( string strLogPath) if exists, return the path if (File.Exists(retFilePath) = true)įileStream fs = new FileStream(retFilePath, search the file below the current directory string retFilePath = baseDir + " //" + " LogFile.txt" get the base directory string baseDir = +Ī / /// Log file path /// private static string GetLogFilePath() / /// Check the log file in applcation directory. Sw = new StreamWriter(strPathName, true) / /// /// /// false if the problem persists /// private static bool WriteErrorLog( string strPathName, / and stack trace information to the text file / /// Write Source,method,date,time,computer,error write the error log to that text file if ( true != WriteErrorLog(strPathName,objException)) If ( false = CheckDirectory(strLogFilePath))įileStream fs = new FileStream(strLogFilePath,įileMode.OpenOrCreate, FileAccess.ReadWrite) If the log file path is not empty but the file // is not available it will create it if ( true != File.Exists(strLogFilePath)) Get Default log file path "LogFile.txt" If (strLogFilePath.Equals( string.Empty))
/ /// false if the problem persists /// private static bool CustomErrorRoutine(Exception objException) / If the Log File path is not empty but the file is / If the LogFile.txt is not availble it will create it / LogFile.txt under application directory.
Otherwise, error informationĬopy Code /// /// If the LogFile path is empty then, it will write the log entry to Windows event log on the administrator's machine. Parameter bLogType = true capture error information using First one is Boolean, to decide window event log or customized error log. The main function ErrorRoutine will take two parameters. Here, LogFilePath property is used set the error logįile path, where we want to create/reside that file. If ( false = CustomErrorRoutine(objException)) If (!EventLog.SourceExists(EventLogName))ĮventLog.CreateEventSource(objException.Message, Write to Windows event log if ( true = bLogType) Don't process more if the logging if ( false = bLoggingEnabled) Check whether logging is enabled or not bool bLoggingEnabled = false / /// /// /// false if the problem persists public static bool ErrorRoutine( bool bLogType,Exception objException) / /// Write error log entry for window event if the bLogType is true.
/ null then it will update error info into LogFile.txt under Will update the error info into that file with necessary information.Ĭopy Code public static string strLogFilePath = string.Empty If the file is not available, then Logger will create a log file named The main idea is create a error log file based on input we have given. You can use any one of these error capturing mechanisms to monitor your
Register the errors in Windows event log on the administrator's.Creating customized error log file capturing for our application errors.This project will handle the following two steps: Just download, add reference to your application, write single line of code This class demonstrates the use of following namespaces.
This article is demonstrating how to create a text based error log fileĪlong with error messages with your own format, using a C# class. Registered as a log entry in the Windows Event Log on the administrator's Logger is used for creating customized error log files or an error can be