5/04/2015

Sử dụng thư viện log4net


Phần 1 : Hướng dẫn cài đặt

+ Bước 1: Thực hiện Download log4net tại địa chỉ: http://logging.apache.org/log4net/download_log4net.cgi

+ Bước 2: Vào thư mục vừa download về máy và tìm file log4net.dll tương ứng với phiên bản .Net Framework đang làm project sau đó thực hiện Add Reference
Hình 1: Lựa chọn phiên bản .Net Framework tương ứng với project

Hình 2: Thực hiện Add Reference file log4net.dll vào project

+ Bước 3: Cấu hình file Assemply của project chọn Properties/AssemblyInfo.cs và thêm dòng sau vào cuối file.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]

+ Bước 4: Cấu hình file Web.config

+ Chèn cặp thẻ <configSections>: vào trong cặp thẻ </configuration>:
(Thẻ <configSections>: phải là nốt con đầu tiên trong thẻ </configuration>:)

Trong cặp thẻ <configSection>: thực hiện chèn nội dung sau:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
Tiếp tục thêm đoạn code sau vào bên trong thẻ đóng </configuration>:

<log4net>
    <appender name="CoreAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/Logs.log"/>
      <appendToFile value="true"/>
      <maximumFileSize value="3072KB"/>
      <maxSizeRollBackups value="1"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss,fff} %thread - %message%newline%newline----------------------------------------------------------------------------------------------------------------------------%newline%newline"/>
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="CoreAppender"/>
    </root>
  </log4net>

  • Ở đây file value là đường dẫn file log của bạn (chú ý thư mục Logs bạn tự tạo, file Logs.log do log4net sinh ra)
  • MaximunFileSize là kịch thước tối đa của file log
  • Thẻ layout để định dạng cho cấu trúc trong file Log của bạn
  • VD ở đây file Log sẽ được sinh ra với nội dung như sau:



II.Phần II: Cách sử dụng

Bước 1 : Using log4net

o   using log4net;

Bước 2 : Khai báo

    Khai báo 1 đối tượng của log4net (biến toàn cục)
o    protected ILog Logger = LogManager.GetLogger(typeof(<<Class>>));
o   <<Class>> là tên Class của bạn
o   VD:
o    protected ILog Logger = LogManager.GetLogger(typeof(CustomerDao));
 

Bước 3Sử dụng

  • Log4net cung cấp nhiều kiểu logging.
  • Có thể log dạng warning, error, fatal…
  • Thông thường ta sẽ log dạng error như sau:
    • Logger.Error(“<<Message>>”);
    • Message là thông tin của lỗi.
VD:
try{      string sql = string.Format("SELECT *  From {0} where {1}=@Id",DBSchemas.TABLE_CUSTOMER, Customer.FIELD_ID);
      var param = new Collection<SqlParameter> {new SqlParameter("@Id", id)
};      using (IDataReader reader = SqlHelper.ExecuteReader(sql, param))      {          if (reader==nullreturn null;          while (reader.Read())          {               return ExtractDataRaw(reader);          }      }      return null;}catch (SqlException ex)
{     Logger.Error(ex);      return null;}           

    Xem thêm:

    1. Source code tham khảo: Download




    No comments:

    Post a Comment