LanceZhang’s Blog

Dear friends, Welcome to my blog.

A common Log class for .NET



1. Output(string)

This method is used for record the normal Logs, it will create a file name like “log_20090408.txt” to the root directory of your application, and append the log message:



Just like the Output method, this method not only output the log message, but also output the following messages:

  • File name
  • Method name
  • Line number
  • Column number

which is useful for an exception analysis


int i = 8;
int j = 0;
int c = i / j;
catch (Exception ex)

Then, the output is like:



/**//****************************** Module Header ******************************
* Module Name:    Log.cs
* Project:    LanceZhang.Common
* Copyright (c) Lance Zhang (

* History:
* * 3/7/2009 2:50 Lance Zhang Created

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace LanceZhang.Common
public static class Log
private static object lockobj=new object();

private static void WriteToFile(string filename, string content)
using (StreamWriter sw = new StreamWriter(filename, true))


private static string GetDateTime()
return [ + DateTime.Now.ToString(HH:mm:ss+ ;

public static void Output(string msg)
lock (lockobj)
+ @”\log_ + DateTime.Now.ToString(yyyyMMdd+ .txt, GetDateTime() + msg);


public static void OutputError(string msg)
lock (lockobj)
                StackFrame sf 
= new StackTrace(true).GetFrame(1);
                StringBuilder sb 
= new StringBuilder(GetDateTime());
Error from:);
+ @”\log_ + DateTime.Now.ToString(yyyyMMdd+ .txt,  sb.ToString());




April 8, 2009 - Posted by | Code and Solution, Develop Resource |


  1. Jony is my friend
    and [url=]ioana[/url] is my girl

    Comment by neancedaw | May 1, 2009 | Reply

  2. Hi Mr.Zhang,

    This is Bin, a recruitment consultant. We are looking for .NET and JAVA developers for VOLVO IT. It’s one of Global 500 companies headquartered in Sweden. They offer tempting benefits and great possibilities for promotion. If you are interested or have any questions, please feel free to get in touch with me at


    Comment by bin | November 29, 2010 | Reply

  3. Bravo, very good thought

    Comment by Тимур | March 9, 2011 | Reply

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: