logging file ด้วย Microsoft Enterprise Library ตอน 2
9.ในหัวข้อ Logging Application Block ->Trace Listeners ตรงส่วนนี้จะเป็นตัวจัดการ Listeners ของระบบ logs ทางด้านขวามือ
- 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 ทางด้านขวามือ
-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}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
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 & Warnings">
<listeners>
<add name="Error Rolling Flat File Trace Listener" />
<add name="Info Rolling Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration> |


1 Trackback(s)