How to Write a Log in .Net Application.

In Application development environment we always require to track our application performance as well as error and bugs.To tracking bugs and error we always write the log in temporary folder.So today we are going to discuss how to write the log in .Net application.Writing log is not save our time as well as helps us to track our application in proper manner

Today in this blog i will tell you how to write the log using C# programming Language.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class WriteLogFile
    {
        public static bool WriteLog(string strFileName,string strMessage)
        {
            try
            {
FileStream objFilestream =new FileStream(string.Format("{0}\\{1}", Path.GetTempPath(), strFileName)
,FileMode.Append, FileAccess.Write);
StreamWriter objStreamWriter = new StreamWriter((Stream)objFilestream);
objStreamWriter.WriteLine(strMessage);
objStreamWriter.Close();
objFilestream.Close();
return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }

Call the above WriteLog() method any app and pass parameter it will write the Log automatically.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s