2017-06-21 20 views
5

Tôi có môi trường Multi-Threaded nơi ứng dụng bắt đầu từ WPF, khởi tạo 2 luồng công nhân chủ đề một máy chủ web socket và một máy chủ Web API.Sử dụng Log4Net trong môi trường đa luồng

Tôi muốn ghi nhật ký tất cả các sự kiện trong một tệp nhật ký. Tôi đang sử dụng log4Net (lần đầu tiên).

Tôi đang tạo 3 bản sao của Trình ghi nhật ký và giả định rằng trình bổ sung tệp sẽ hiểu rằng nó phải ghi vào một tệp duy nhất.

Code:

Logger Class:

public class Logger :ILogger 
{ 
    private static ILog log = null; 
    static Logger() 
    { 
     log = LogManager.GetLogger(typeof(Logger)); 
     GlobalContext.Properties["host"] = Environment.MachineName; 
    } 
    public Logger(Type logClass) 
    { 
     log = LogManager.GetLogger(logClass); 
    } 

    #region ILogger Members 
    public void LogException(Exception exception) 
    { 
     if (log.IsErrorEnabled) 
      log.Error(string.Format(CultureInfo.InvariantCulture, "{0}", exception.Message), exception); 
    } 
    public void LogError(string message) 
    { 
     if (log.IsErrorEnabled) 
      log.Error(string.Format(CultureInfo.InvariantCulture, "{0}", message)); 
    } 
    public void LogWarningMessage(string message) 
    { 
     if (log.IsWarnEnabled) 
      log.Warn(string.Format(CultureInfo.InvariantCulture, "{0}", message)); 
    } 
    public void LogInfoMessage(string message) 
    { 
     if (log.IsInfoEnabled) 
      log.Info(string.Format(CultureInfo.InvariantCulture, "{0}", message)); 
    } 
    #endregion 
} 

Log Config:

<log4net debug="true"> 
    <appender name="RollingLogFileAppender" 
       type="log4net.Appender.RollingFileAppender"> 
    <file value="/pathToLogFile" /> 
    <staticLogFileName value="false"/> 
    <appendToFile value="true" /> 
    <rollingStyle value="Date" /> 
    <datePattern value=" yyyy-MM-dd&quot;.txt&quot;"/> 
    <layout type="log4net.Layout.PatternLayout"> 
     <!--<param name="ConversionPattern" 
       value="%d [%t] %-5p %c %m%n" />--> 
     <conversionPattern 
     value="%date [%thread] %-5level %logger ==> %message%newline"/> 
    </layout> 
    </appender> 

    <root> 
    <level value="INFO" /> 
    <appender-ref ref="RollingLogFileAppender" /> 
    <appender-ref ref="AdoNetAppender" /> 
    </root> 
</log4net> 

tôi đang tạo ra 3 trường hợp Logger, trong 3 dự án khác nhau như:

static ILogger logger = new Logger(typeof(MainWindow)); 
static ILogger logger = new Logger(typeof(DefaultApiController)); 
static ILogger logger = new Logger(typeof(WebSocket)); 

Nhưng, Tệp nhật ký chỉ có nhật ký WebSocket. Có một lỗ hổng trong sự hiểu biết của tôi? Tôi làm sai ở đâu?

Về cơ bản, có 3 luồng đang chạy, vì vậy chắc chắn tôi không thể chuyển trong cùng một đối tượng Logger từ một đến khác.

+1

Tại sao trình bao bọc? Có vẻ như nó chỉ trùng lặp chức năng (lọc theo cấp độ đăng nhập) đã tồn tại trong log4net - bạn thậm chí còn sử dụng cấu hình của log4net để làm điều đó. – Adrian

Trả lời

7

Vấn đề chính của bạn là bạn đang sử dụng trình ghi nhật ký nhưng cố gắng gán biến tĩnh 3 lần khác nhau trong hàm tạo của cá thể, biến cuối cùng sẽ luôn thắng. Số lượng dự án không quan trọng, nếu tất cả các dự án được lưu trữ trong cùng một tệp thực thi sẽ chỉ có 1 phiên bản Logger::log (giả sử bạn không tạo nhiều miền ứng dụng).

public class Logger :ILogger 
{ 
    private static ILog log = null; 
    static Logger() 
    { 
     log = LogManager.GetLogger(typeof(Logger)); 
     GlobalContext.Properties["host"] = Environment.MachineName; 
    } 
    public Logger(Type logClass) 
    { 
     log = LogManager.GetLogger(logClass); 
    } 

Nên

public class Logger :ILogger 
{ 
    private ILog log = null; 
    public Logger(Type logClass) 
    { 
     log = LogManager.GetLogger(logClass); 
    } 

Điều đó nói rằng tôi không chắc chắn lý do tại sao bạn đang cố gắng để thiết lập nó theo cách này. Nó dễ dàng hơn nhiều để cung cấp một logger tĩnh trong bất kỳ lớp nào cần một. Ví dụ:

public class SomeInterestingType { 
    private static readonly ILog Logger = LogManager.GetLogger(typeof(SomeInterestingType)); 

    public void DoSomething() { 
    Logger.Info("Busy doing something"); 
    } 
} 

Và sau đó chỉ cần lặp lại mẫu đó.

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