블로그 이미지
따시쿵

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# log class'에 해당되는 글 1

  1. 2015.02.21 텍스트 로그 파일 라이브러리 - 2
2015. 2. 21. 07:21 C#

Trace class를 이용해서 파일에 로그 파일을 쓰는 경우가 있습니다. 이런 경우에도 설정 파일(app.config)을 이용해서 파일에 쓰는 경우를 보여 줍니다.


Trace.WriteLine 을 이용한 경우입니다.

System.IO.StreamWriter class 와 로그 파일에 lock mode 를 걸지 않는 경우입니다.


app.config


  <!--Trace.WriteLine 을 설정하는 부분 시작 -->
  <system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializedata="TextWriterOutput.log" />
        <remove name="Default" />
      </remove>
    </trace>
  </system.diagnostics>
 <!--Trace.WriteLine 을 설정하는 부분 종료 -->


LoggerSystem.cs


    #region LoggerSystem class
    public static class LoggerSystem
    {
        public static void Error(string message, string module)
        {
            WriteEntry(message, "error", module);
        }

        public static void Error(Exception ex, string module)
        {
            WriteEntry(ex.Message, "error", module);
        }

        public static void Warning(string message, string module)
        {
            WriteEntry(message, "warning", module);
        }

        public static void Info(string message, string module)
        {
            WriteEntry(message, "info", module);
        }

        private static void WriteEntry(string message, string type, string module)
        {
            Trace.WriteLine(
                    string.Format("[{0}],{1},{2},{3}",
                                  DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                  type,
                                  module,
                                  message));
        }
    }
    #endregion


사용예제


            new Thread(delegate()
                {
                    for (int i = 0; i <= 1000; i++)
                    {
                        LoggerSystem.Error("button1_Click = " + i.ToString(), "myapp");
                    }
                }).Start();

            new Thread(delegate()
            {
                for (int i = 0; i <= 1000; i++)
                {
                    LoggerSystem.Info("button2_Click = " + i.ToString(), "myapp");
                }
            }).Start();  


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

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