logging file ด้วย Microsoft Enterprise Library ตอน 3
การติดตั้ง Microsoft enterprise library ในโปรเจค
1.ที่โปรเจคให้ทำการเลือก Add Reference
2.เลือก Microsoft.Practices.EnterpriseLibrary.Logging.dll จาก directory ที่เรา install program เอาไว้ใน (ดูการปรับแต่งข้อ1)
3.เมื่อทำการ add reference เสร็จเรียบร้อยจะได้ดังรูป
การใช้งาน Microsoft enterprise library
ตัวอย่าง code ที่ใช้สร้าง logs file
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
namespace Gmm.Logfile
{
/// <summary>
/// write text logger class
/// </summary>
public class LoggerHandle
{
private String Timestamp { get; set; }
private String Message { get; set; }
private String Category { get; set; }
private int Priority { get; set; }
private String Title { get; set; }
private String TransactionId { get; set; }
private System.Diagnostics.TraceEventType Severity { get; set; }
private static String INFO_CATALOG = "General";
private static String ERROR_CATALOG = "General";
private static String WARNING_CATALOG = "General";
private static String DEFAULT_CATALOG = "General";//DEFAULT catagory
LogEntry log = null;
public LoggerHandle() {
this.Category = DEFAULT_CATALOG;
}
public LoggerHandle(String TitleName):base()
{
this.Title = TitleName;
}
public LoggerHandle(Type TitleType)
: base()
{
this.Title = TitleType.ToString();
}
public void setTransaction(String transactionId) {
this.TransactionId = transactionId;
}
public void Info(String messageInfo) {
this.Message = messageInfo;
this.Severity = TraceEventType.Information;
this.Category = INFO_CATALOG;
this.Priority = 2;
writeLog();
}
public void Warn(String messageInfo)
{
this.Message = messageInfo;
this.Severity = TraceEventType.Warning;
this.Category = WARNING_CATALOG;
this.Priority = 3;
writeLog();
}
public void Error(String messageInfo)
{
this.Message = messageInfo;
this.Severity = TraceEventType.Error;
this.Category = ERROR_CATALOG;
this.Priority = 5;
writeLog();
}
public void Debug(String messageInfo)
{
this.Message = messageInfo;
this.Severity = TraceEventType.Error;
this.Category = ERROR_CATALOG;
this.Priority = 5;
writeLog();
}
private void writeLog() {
log = new LogEntry();
log.Message = this.Message;
log.Title = this.TransactionId + ":" + this.Title;
log.Priority = this.Priority;
log.Severity = this.Severity;
log.Categories.Add(this.Category);
log.TimeStamp = DateTime.Now;
Logger.Write(log);
}
}//end class
}//end namespace |
จบแล้วครับ กว่าจะเขียนให้จบได้ยาวนานถึง 3 บทความ เหอ ๆ …
Related Post
October 12th, 2008 |
by pat@codeburning.com |
Published in
.NET



1 Trackback(s)