블로그 이미지
따시쿵

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

'c# logfile library'에 해당되는 글 1

  1. 2015.01.09 텍스트 로그 파일 라이브러리 - 1
2015. 1. 9. 10:22 C#

윈도우/콘솔 프로그램 중 로그 파일을 남기고 싶을 경우가 종종 있습니다.


클라이언트 경우에는 서버로 데이타를 전송한 목록을 가지고 있다거나 서버에서는 클라이언트에서 전송 된 데이타를 정상적으로 받았는지 확인 해 볼 필요가 있습니다. 이런 경우 텍스트 파일로 남기는 경우가 필요한데 이런 경우에 사용하면 됩니다.


멀티 쓰레드 환경에서 작동하도록 작성했으며 라이브러리와 사용 예제를 올립니다.


실행파일 폴더를 가져와서 Log 폴더를 작성하고, 일자별로 파일을 생성하며 시간별로 로그를 쌓습니다.



LogFile.cs


LogFile.cs


using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Diagnostics;

/// 
/// Summary description for LogFile
/// 
public class LogFile
{

    private static string strLogDir = System.IO.Directory.GetCurrentDirectory() + @"\Log\";
	private static string strLogFile = "Log";
    private static readonly object thisLock = new object();

	public LogFile()
	{
	}

	public static void SetLogPath(string strDir)
	{
		strLogDir = strDir;
	}

	public static void SetLogFile(string strFile)
	{
		strLogFile = strFile;
	}

	public static void WriteLog(string strLog)
	{
		WriteLog(strLog, LogCode.Infomation, strLogFile);
	}

	public static void WriteLog(string strLog, string strFileName)
	{

		WriteLog(strLog, LogCode.Infomation, strFileName, strLogDir);
	}

	public static void WriteLog(string strLog, string strFileName, string strPath)
	{
		WriteLog(strLog, LogCode.Infomation, strFileName, strPath);
	}

	public static void WriteLog(string strLog, LogCode logCode)
	{
		WriteLog(strLog, logCode, strLogFile);
	}

	public static void WriteLog(string strLog, LogCode logCode, string strFileName)
	{

		WriteLog(strLog, logCode, strFileName, strLogDir);
	}

	public static void WriteLog(string strLog, LogCode logCode, string strFileName, string strPath)
	{
		string strFullName;

		if (!Directory.Exists(strPath))
		{
			Directory.CreateDirectory(strPath);
		}

		if (strPath.EndsWith(@"\") == false || strPath.EndsWith("/") == false)
		{
			strPath = strPath + @"\";
		}
		strFullName = strPath + strFileName + "_" + DateTime.Now.ToShortDateString() + ".txt";

		string strFullLog = DateTime.Now.ToString("HH:mm:ss") + " (" + logCode.ToString() + ")" + " : " + strLog;

        lock(thisLock)
        { 
		    StreamWriter sw = new StreamWriter(strFullName, true, System.Text.Encoding.UTF8, 4096);
		    sw.WriteLine(strFullLog);
		    sw.Close();
        }
	}

	public enum LogCode
	{
		Infomation = 0,
		Success = 1,
		Error = -1,
		Failure = -2,
		Warning = -10,
		SystemError = -101,
		ApplicationError = -201
	}
}


사용예제


            new Thread(delegate()
                {                    
                    for (int i = 0; i <= 100; i++)
                        LogFile.WriteLog(string.Format("Thread[1] = {0}", i.ToString()));

                }).Start();

            new Thread(delegate()
                {
                    for (int i = 0; i <= 100; i++)
                        LogFile.WriteLog(string.Format("Thread[2] = {0}", i.ToString()));

                }).Start();;


'C#' 카테고리의 다른 글

텍스트 로그 파일 라이브러리 - 2  (0) 2015.02.21
멀티플(multiple) 윈도우 - 1  (0) 2015.01.24
멀티스레드 환경에서 UI 에 데이타 표시할 경우  (0) 2015.01.15
Task 클래스  (0) 2015.01.10
ManualResetEvent 클래스  (0) 2014.12.24
posted by 따시쿵
prev 1 next