The performance of our applications, as well as errors and bugs, must always be tracked in an application development environment. We always keep a log of any errors and defects in a temporary folder. Therefore, today’s topic will be how to write a log in a.Net application. The main source of data for network observability is a log file, which is a computer-generated data file containing details about usage patterns, activities, and processes within an operating system, application, server, or other device.
I will therefore explain how to write the log using the C# programming language in this blog.
Lets proceed and create the Log file.
Step 1 :
Gets or sets the file directory path to the location of the file where the events are stored for the log.
<appSettings> <!-- Log File--> <add key="LogFile" value="D:\Temp\log" /> <add key="Logdays" value="5" /> //Store the number of days after which you want to delete the logs. </appSettings>
Step 2:
We shall determine whether the Log file is there or not using the StringBuilder . If one doesn’t already exist, we’ll make one and add the text to the Log file.
private string LogPath; private int Logdays = 0; public void Write(Exception ex) { LogPath = System.Configuration.ConfigurationManager.AppSettings["LogFile"]; Logdays = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["Logdays"]); WriteLog(LogPath, ex.Message, ex.StackTrace); } public void WriteLog(string exceptionSource, string message, string stackTrace) { LogPath = System.Configuration.ConfigurationManager.AppSettings["LogFile"]; Logdays = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["Logdays"]); exceptionSource = LogPath; WriteText(exceptionSource, message, stackTrace); }
Step 3 :
The Log file can be formatted according to our specifications for what and how to write.
public void WriteText(string exceptionSource, string message, string stackTrace) { _logFile = _getLogFile(); StringBuilder sb = new StringBuilder(); sb.AppendLine("------------------"); sb.Append(DateTime.Now.ToString()); sb.Append("------------------"); sb.AppendLine(eventLogType.ToString()); if (!string.IsNullOrEmpty(exceptionSource)) { sb.AppendLine(exceptionSource); } if (!string.IsNullOrEmpty(message)) { sb.AppendLine(message); } if (!string.IsNullOrEmpty(stackTrace)) { sb.AppendLine(stackTrace); } File.AppendAllText(_logFile + ".txt", sb.ToString()); } private string _getLogFile() //Create syncLog folder if not exists { if (!Directory.Exists(LogPath)) { Directory.CreateDirectory(LogPath); } string filePath; filePath = LogPath + @"Log_" + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day; return filePath; }
Step 4 :
To generate the Log file, let’s now invoke the WriteLog() function.
protected void Page_Load(object sender, EventArgs e) { string LogPath = System.Configuration.ConfigurationManager.AppSettings["LogFile"]; int Logdays = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["Logdays"]); WriteLog(LogPath, "Getting the info", ""); }