我正在使用log4net进行C#SDK,请参阅以下代码片段(示例)以了解我现有的实现(SDK和客户端)。
我想在SDK中插入可插入的日志。
SDK在内部使用log4net,但客户端无法将其替换为其他日志框架。
我想有一个ILogger接口让客户端选择是否覆盖它。
在不影响SDK现有用户的情况下,您能否帮助您了解如何在C#2.0 Framework / Visual Studio 2005中实现这一目标?
如何编写ILog接口并启用覆盖?
请帮忙。
// SDK Code Snippet
// LogManagerWrapper
using System;
using log4net;
namespace SDK
{
public static class LogManagerWrapper
{
public static ILog GetLogger(Type type)
{
if (LogManager.GetCurrentLoggers().Length == 0)
{
LoadConfig();
}
return LogManager.GetLogger(type);
}
private static void LoadConfig()
{
log4net.Config.XmlConfigurator.Configure();
}
}
}
// Client Code Snippet
using System;
using log4net;
using SDK;
namespace Client
{
class Program
{
private static readonly ILog logger = LogManagerWrapper.GetLogger(typeof(Program));
private void Method()
{
try
{
Random rndm = new Random();
int number = rndm.Next(1000);
decimal Money = 100 / number;
decimal Cash = 100 / Money;
}
catch(Exception ex)
{
logger.Debug(ex.Message);
}
}
static void Main()
{
logger.Info("Start Time: " + DateTime.Now.ToString("dddd, MMMM dd, yyyy h:mm:ss tt"));
Program prgm = new Program();
prgm.Method();
logger.Info("End Time: " + DateTime.Now.ToString("dddd, MMMM dd, yyyy h:mm:ss tt"));
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
// Client App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="ClientLog.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
答案 0 :(得分:0)