Wednesday, December 15, 2010

Write log file while creating application



Write log while creating ur application. use below two class for write a log for ur appliaciton
//**************************************************************************//


Create one Application Class for Address where to store log………….

/******************/

using System;
using System.Collections.Generic;
using System.Text;

   
        class AppClass
        {
// change folder name if require by default mylog in c drive
            //public static string AppPath = @"c:\myLog\";
           // public static string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            public static string AppPath = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);

            public AppClass()
            {
                Log4NET.Write(this.ToString() + " AppPath " + AppPath);
           
            }
        }


//**************************************************************************/


//  Create another Class for write log as per Datetime

using System;
using System.Collections.Generic;
using System.Text;


     public  class Log4NET
        {
            public static string logfile = @"\logs\LogEntery.log";
            public static string errlogfile = @"\logs\LogError.err";
            
            public static void Write(string message)
            {
                string DateTimeFile = "";
                DateTime dt = DateTime.Now;
                try{
                        if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                        {
                            System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                        }
                        DateTimeFile = dt.Year + "" + (dt.Month <= 9 ? "0" + dt.Month + "" : dt.Month + "") + "" + dt.Day + "";
                        logfile = @"\logs\DBConfig_" + DateTimeFile + ".log";
                        System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + logfile, System.IO.FileMode.Append);
                        byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                        fs.Write(info, 0, info.Length);
                        fs.Close();
                    }catch(Exception se){
                        if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                        {
                        System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                        }
                    System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + @"\logs\dbLogCon.err", System.IO.FileMode.Append);
                        byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                        fs.Write(info, 0, info.Length);
                        fs.Close();
                    }

            }
            public static void Error(string message)
            {
                string DateTimeFile = "";
                DateTime dt = DateTime.Now;
                try{
                    if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                    {
                        System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                    }
                    DateTimeFile = dt.Year + "" + (dt.Month <= 9 ? "0" + dt.Month + "" : dt.Month + "") + "" + dt.Day + "";
                    errlogfile = @"\logs\DBConfig_" + DateTimeFile + ".err";
                    System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + errlogfile, System.IO.FileMode.Append);
                    byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                    fs.Write(info, 0, info.Length);
                    fs.Close();
                    }catch(Exception se){
                        if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                        {
                            System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                        }
                        System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + @"\logs\dbLogCon.err", System.IO.FileMode.Append);
                        byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                        fs.Write(info, 0, info.Length);
                        fs.Close();                    
                    }

            }

        }


//**********************************************************************//



//***********************how to use in ur application**************************//

protected void btnLogin_Click(object sender, EventArgs e)
    {
       Try
       {
                Log4NET.Write("Login Clicked:");
 
      }
     Catch(Exception er)
       {
             Log4NET.Error("Login Failed:" +er.ToString());
        }
      
    }


//*******************//
//File Created on above path and log is written.......
logfile



Thank you bhavesh
 
ads

We are updating our database daily keep watching.
ra one - mausam- Force - Rascals- Mere Brother Ki Dulhan -Aarakshan- Bodyguard

16 comments:

Anonymous said...

I seldom leave comments on blog, but I have been to this post which was recommended by my friend, lots of valuable details, thanks again.

Anonymous said...

a new thing arrives
Http://www.songsquery.com

Anonymous said...

It is my pleasure to say you that all of your articles are superb and I really love the way you put each and every sentence. You will be rated 8.75 out of 10. Brilliant work,keep it up. Your grammatical sense is simply outstanding. Carry on the good work.And yes i have bookmarked your site sqlclass3.blogspot.com .

Anonymous said...

Thanks for taking the time to discuss this, I really feel strongly about it and love learning more on this topic. If potential, as you achieve expertise, would you thoughts updating your blog with additional data? It is extremely helpful for me.

Anonymous said...

Need to keep testing my blog. Not working as I want it to yet. Thanks for the theme. Maybe this will get mine to look better.

Anonymous said...

tramadol online no prescription where to buy tramadol online - tramadol dosage people

Anonymous said...

buy tramadol online tramadol online no prescription overnight delivery - high on tramadol hcl

Anonymous said...

buy tramadol online tramadol hcl 50 mg and tylenol - how to buy tramadol online overnight

Anonymous said...

xanax online generic xanax pills identification - xanax cures anxiety

Anonymous said...

cheap tramadol tramadol 50mg how many can i take - tramadol depression

Anonymous said...

tramadol generic tramadol addiction how long - tramadol vs codeine high

Anonymous said...

xanax pills xanax bars in system - buy cheap xanax online with no prescription

Anonymous said...

buy cheap tramadol tramadol 50 mg for dogs - buy tramadol ultram

Anonymous said...

xanax online interaction of xanax and alcohol - xanax online to australia

Anonymous said...

buy tramadol online can dog overdose tramadol - tramadol withdrawal duration symptoms

Anonymous said...

buy tramadol online no prescription overnight tramadol dosage feline - tramadol hcl 300 mg