Tôi đang cố gắng làm điều này tối nay. Tôi thấy Commons.Logging nói rằng nó có định tuyến sự kiện hai hướng giữa các thư viện đăng nhập.
- Sử dụng NuGet thêm Common.Logging.log4net và Common.Logging.NLog (mà sẽ nhận được log4net và NLog qua gói phụ thuộc)
- Thêm cấu hình dưới đây.
- Trong
Main()
khởi tạo log4net sử dụng log4net.Config.XmlConfigurator.Configure();
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets async="true">
<target name="file" xsi:type="File" fileName="d:\logs\app1\logging.txt"/>
<target name="console" xsi:type="ColoredConsole" />
</targets>
<rules>
<logger name="*" writeTo="file"/>
<logger name="*" writeTo="console"/>
</rules>
</nlog>
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
<log4net>
<!-- Commons.Logging will bridge the log4net messages to NLog, required to see TopShelf log messages -->
<appender name="CommonLoggingAppender" type="Common.Logging.Log4Net.CommonLoggingAppender, Common.Logging.Log4Net">
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="%level - %class.%method: %message" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="CommonLoggingAppender" />
</root>
</log4net>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Tài liệu tham khảo: Common.Logging Bridging Logging Systems
Full sự kiện bi-directional định tuyến hỗ trợ cho EntLib 3.1, EntLib 4.1, log4net 1.2.9, log4net 1.2 .10 và NLog ghi nhật ký
Tôi sẽ thả NLog hoàn toàn. Nó đã được quảng cáo như là một lựa chọn tốt hơn, nhưng tác giả của nó ngừng làm việc sau khi được Microsoft thuê, thật là xấu hổ .. Tôi bắt đầu sử dụng log4net sau này và tôi thích nó hơn, có thể xem tài liệu và xem bản thân bạn. –
Cảm ơn gợi ý ... Tôi sẽ xem xét sử dụng log4net cho ứng dụng của tôi thay vì – Martin
NLog dường như hoạt động rất nhiều :-) –