2013-08-08 35 views
20

Tôi vừa cài đặt Elmah (https://code.google.com/p/elmah/) cho ứng dụng ASP.NET của mình. Có thể đăng nhập một tin nhắn mà không tạo ngoại lệ trước không?Ghi nhật ký lỗi Elmah, tôi có thể chỉ cần đăng nhập một tin nhắn không?

catch(Exception e) 
{ 
    Exception ex = new Exception("ID = 1", e); 
    ErrorSignal.FromCurrentContext().Raise(ex); 
} 

Vậy là nó có thể làm:

ErrorSignal.FromCurrentContext().log("Hello I am testing Elmah"); 

Trả lời

32

Có, bạn có thể sử dụng mà không cần ErrorSignal ném một ngoại lệ.

ErrorSignal.FromCurrentContext().Raise(new NotSupportedException()); 

Để có thông báo tùy chỉnh, bạn có thể tạo ngoại lệ tùy chỉnh.

var customEx = new Exception("Hello I am testing Elmah", new NotSupportedException()); 
ErrorSignal.FromCurrentContext().Raise(customEx); 
+0

cảm ơn bạn đã trả lời nhanh – user603007

11

Hãy thử điều này

Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Your message")); 
1

Tôi biết đây là một câu hỏi cũ, nhưng nếu bạn không muốn tạo ra một ngoại lệ, bạn cũng có thể sử dụng

var error = new Error 
{ 
    Source = eventType.ToString(), 
    Type = $"Trace-{eventType}", 
    Message = message, 
    Time = DateTime.UtcNow 
}; 

ErrorLog.GetDefault(HttpContext.Current).Log(error); 

như trong this answer .

Các vấn đề liên quan