C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Best Practices : Write Log File or Exception File


Posted Date: 30 Sep 2008    Resource Type: Articles    Category: General

Posted By: Mathew Philip       Member Level: Gold
Rating:     Points: 10




Loging is a common procedure to view the happenings in future, It will help us to check the flow of the program or software and also helps us to track errors occured while processing.

So I recommend you to use a log fiel to store related information. The file name can be constructed according to the date or can be a single file with predefined name.

Another advantage of loging is thath we can request the client to send the log file to view the happenings at client side.

Here is a simple log file creation and appending code.. Hope it will help you to make a better software



using System,IO;

public void WriteLog(string message)
{//MESSAGE TO WRITE IN THE LOG, THIS MAY BE AN EXCEPTION MESSGE OR PROGRAMMER DEFINED MESSAGE
try
{
string fileNameWithPath = "";

fileNameWithPath = @"C:\MyFolder\MyLogFile.txt"; //INSTED OF THIS YOU CAN DYNAMICALLY MADE THE FILENAME

if (!(string.IsNullOrEmpty(fileNameWithPath)))
{

if (!(string.IsNullOrEmpty(message)))
{
FileStream fileStream = new FileStream(fileNameWithPath,
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter objStreamWriter = new StreamWriter(fileStream);
objStreamWriter.BaseStream.Seek(0, SeekOrigin.End);
message = " -> " + DateTime.Now.ToLongDateString() + " At " +
DateTime.Now.ToLongTimeString() + " : " + message;
objStreamWriter.WriteLine(message);
objStreamWriter.Flush();
objStreamWriter.Close();
}
}

}
catch (Exception logException)
{

}

}



NOTE : you need proper rights to create file in hard drive




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Log file  .  File write  .  Exception log  .  Best practices  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: avoiding Dll hell in .Net
Previous Resource: Debuging Windows service
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween masks

Contact Us    Privacy Policy    Terms Of Use