Results 1 to 3 of 3

Thread: Best way to create a log file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Best way to create a log file

    Hello,

    I am developing an application for a client. They have requested some logging information.

    I am thinking about be able to log debug messages, information, and serious errors.

    Is there a way I could automatically switch the ones I want to log? For example, if I just wanted to write information to the log file, or write debug and information, or just serious errors.

    What is the best practice when creating and writing to log files?

    Many thanks for any advice,
    steve

  2. #2
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: Best way to create a log file

    hey steve_rm
    use following class for loggin purpose
    just make a private or global object of cLogger and set as many information you want

    :-)

    vb Code:
    1. class cLogger
    2.     {
    3.         private string m_LogFileName;
    4.         private string m_LogPath;
    5.         private bool m_Logging;
    6.         private string m_Area;
    7.         private string m_ClassName;
    8.         private string m_FunctionName;
    9.         private StreamWriter oStreamWriter;
    10.  
    11.         public string LogFileName
    12.         {
    13.             get
    14.             {
    15.                 return m_LogFileName;
    16.             }
    17.             set
    18.             {
    19.                 m_LogFileName = value;
    20.             }
    21.         }
    22.  
    23.         public string LogPath
    24.         {
    25.             get
    26.             {
    27.                 return m_LogPath;
    28.             }
    29.             set
    30.             {
    31.                 m_LogPath = value;
    32.                 if (m_LogPath[m_LogPath.Length - 1].ToString() != @"\") m_LogPath = m_LogPath + @"\";
    33.             }
    34.         }
    35.  
    36.         public bool Logging
    37.         {
    38.             get
    39.             {
    40.                 return m_Logging;
    41.             }
    42.             set
    43.             {
    44.                 m_Logging = value;
    45.             }
    46.         }
    47.  
    48.         public string Area
    49.         {
    50.             get
    51.             {
    52.                 return m_Area;
    53.             }
    54.             set
    55.             {
    56.                 m_Area = value;
    57.             }
    58.         }
    59.  
    60.         public string ClassName
    61.         {
    62.             get
    63.             {
    64.                 return m_ClassName;
    65.             }
    66.             set
    67.             {
    68.                 m_ClassName = value;
    69.             }
    70.         }
    71.  
    72.         public string FunctionName
    73.         {
    74.             get
    75.             {
    76.                 return m_FunctionName;
    77.             }
    78.             set
    79.             {
    80.                 m_FunctionName = value;
    81.             }
    82.         }
    83.  
    84.         public cLogger()
    85.         {
    86.             try
    87.             {
    88.                 m_Logging = bGlobalLogging;
    89.                 m_LogPath = sGlobalLogPath;
    90.                 m_LogFileName = sGlobalLogFileName;
    91.             }
    92.             catch // (Exception oEx)
    93.             {
    94.                
    95.                // throw;
    96.             }
    97.         }
    98.  
    99.         public bool WriteLog(string sFunctionName, string sMessage)
    100.         {
    101.             bool bResult = true;
    102.  
    103.             try
    104.             {
    105.                 if (m_Logging == true)
    106.                 {
    107.                     m_FunctionName = sFunctionName;
    108.                     return WriteLog(sMessage);
    109.                 }
    110.             }
    111.             catch //(Exception oEx)
    112.             {
    113.                 bResult = false;
    114.                 //throw;
    115.             }
    116.             return bResult;      
    117.         }
    118.  
    119.         public bool WriteLog(string sMessage)
    120.         {
    121.             bool bResult = true;
    122.  
    123.             try
    124.             {
    125.                 if (m_Logging == true)
    126.                 {
    127.                     string sLogFilePathName = m_LogPath + m_LogFileName;
    128.                     string sContents = string.Empty;
    129.                     sContents = DateTime.Now + "|" + m_Area + "|" + m_ClassName + "|" + m_FunctionName + "|" + sMessage + Environment.NewLine;
    130.                     oStreamWriter = File.AppendText(sLogFilePathName);
    131.                     oStreamWriter.WriteLine(sContents);
    132.                     oStreamWriter.Close();
    133.                 }
    134.             }
    135.             catch //(Exception oEx)
    136.             {
    137.                 bResult = false;
    138.                 //throw;
    139.             }
    140.             return bResult;          
    141.         }
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Best way to create a log file

    Maybe you could keep some flags for that in the database or at application level and in the logger function check for the flags and accordingly log the required events.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width