AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Thursday 26 November 2015

Create log file in windows application

The log file is necessary for tracking bug in an application. If log file is maintained in an application, then you can track bugs without debug an application.

So here is the code for creating a log file.

Code
public static void AddtoLogFile(string Message)
   {
        try
        {
            string LogPath = AppDomain.CurrentDomain.BaseDirectory;
            string filename = "\\Log.txt";
            string filepath = LogPath + filename;
            if (!File.Exists(filepath))
            {
                StreamWriter writer = File.CreateText(filepath);
                writer.Close();
            }
            using (StreamWriter writer = new StreamWriter(filepath, true))
            {
                writer.WriteLine(Message);                    
            }
        }
        catch
        { }
   }

AppDomain.CurrentDomain.BaseDirectory will return the path to running assembly. Add this method to application helper class so that it can call easily.

0 comments :

Post a Comment

  • Popular Posts
  • Comments