logging file ด้วย Microsoft Enterprise Library ตอน 2

 

ต่อจากขั้นตอนที่แล้ว

 

9.ในหัวข้อ Logging Application Block ->Trace Listeners ตรงส่วนนี้จะเป็นตัวจัดการ Listeners ของระบบ logs ทางด้านขวามือ

enterprise-library-7

 

                - Name->Name = ชื่อของ Listeners ตัวนี้

                -General->FileName = ตำแหน่ง path directory ที่เราต้องการให้ทำการจัดเก็บ logs files ไว้

                -General->Filter = รูปแบบการ filter ที่จะใช้ใน listener นี้เช่น Info ,Warning, Error เป็นต้น

                -General->Footer = กำหนดการแสดงผลข้อความที่ตำแหน่งท้ายสุดของ logs files ที่เรา เลือก

                -Formatter = เลือกรูปแบบการแสดงผลจากที่อธิบายไว้ใน 8.

                -General->Header = กำหนดการแสดงผลข้อความที่ตำแหน่งหัวของ logs files ที่เรา เลือก

                -RollFileExistsBehavior = ถ้ามี file นั้นอยู่ก่อนแล้ว ให้ทำอย่างไรกับมัน

                -RollInterval = กำหนดระยะเวลาการสร้าง file ใหม่

                -RollSizeKB = กำหนดขนาดของ file ถ้ามากกว่าที่กำหนด จะทำการ write file ขึ้นมาใหม่

                -TimeStampPattern = รูปแบบของวันที่

 

                9.1 ตัวอย่างรูปแบบใน config เมื่อเราทำการกด save ระบบจะทำการ generate ข้อมูลใส่เพิ่มเข้าไปใน file config ของเราอัตโนมัติ

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<listeners>
      <add fileName="D:\Logs\error\Error.log" footer="" formatter="Error Text Formatter"
        header="----------------------------------------" rollFileExistsBehavior="Increment"
        rollInterval="Midnight" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="Error" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Error Rolling Flat File Trace Listener" />
      <add fileName="D:\Logs\info\Info.log" footer="" formatter="Info Text Formatter"
        header="----------------------------------------" rollFileExistsBehavior="Increment"
        rollInterval="Midnight" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Info Rolling Flat File Trace Listener" />
    </listeners>

 

10.ในหัวข้อ Logging Application Block ->Category Source ตรงส่วนนี้จะเป็นตัวจัดการหมวดหมู่ ของระบบ logs ทางด้านขวามือ

enterprise-library-8

                -ReferencedTraceListener = เลือก listener ที่เราต้องการจากข้อ 9

 

                10.1 ตัวอย่างรูปแบบใน config เมื่อเราทำการกด save ระบบจะทำการ generate ข้อมูลใส่เพิ่มเข้าไปใน file config ของเราอัตโนมัติ

 

1
2
3
4
5
6
7
8
  <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Error Rolling Flat File Trace Listener" />
          <add name="Info Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>

 

11.ตัวอย่าง config ทั้งหมดเมื่อเราทำการเลือกรูปแบบจนเสร็จแล้วจะอยู่ภายใต้ tag <configuration> -ของ file ที่เราได้ทำการเลือกไว้ในข้อ5

 

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
32
33
34
35
36
37
38
39
40
41
42
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="D:\Logs\error\Error.log" footer="" formatter="Error Text Formatter"
        header="----------------------------------------" rollFileExistsBehavior="Increment"
        rollInterval="Midnight" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="Error" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Error Rolling Flat File Trace Listener" />
      <add fileName="D:\Logs\info\Info.log" footer="" formatter="Info Text Formatter"
        header="----------------------------------------" rollFileExistsBehavior="Increment"
        rollInterval="Midnight" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Info Rolling Flat File Trace Listener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Error Text Formatter" />
      <add template="{title}	 {timestamp}	 {severity}	{message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Info Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Error Rolling Flat File Trace Listener" />
          <add name="Info Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Error Rolling Flat File Trace Listener" />
          <add name="Info Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

 

ติดตามต่อในตอนที่ 3

 

 

 

Related Post
October 12th, 2008  |  by pat@codeburning.com  |  Published in .NET


ข้อมูลข่าวสาร

คำค้นยอดนิยม

Copyright Notice

  • สิทธิ์ในการเผยแพร่เป็นของท่าน
  • สิทธิ์ในทางพาณิชย์ไม่ได้เป็นของท่าน
  • สิทธิ์ในการเปลี่ยนแปลงไม่ได้เป็นของท่าน

Creative Commons License
Creative Commons Attribution 3.0