将日志文件写入两个不同的文件中
我的应用程序运行在自定义的客户端框架上,客户端框架使用 log4net 来记录自己的日志文件。 我们(我们的应用程序)必须使用相同的 log4net 在我们自己的路径(比如我们的自定义路径)中记录我们的日志文件。 目前我们的日志文件已创建,但日志未写入该文件。 它正在写入客户端框架日志文件。
我搜索了很多网站的链接 Log4Net:以编程方式指定多个记录器(具有多个文件附加程序) 帮助我以编程方式配置 log4net 配置, 我的日志语句仍然没有写入我的日志文件中。使用的代码如下
public class TraceLog
{
private string message = string.Empty;
private static ILog ILogger = null;
private static TraceLog instance = new TraceLog();
private TraceLog()
{
SetLevel("Log4net.MainForm", "ALL");
AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender", "C:\\mylog.log"));
}
public static TraceLog Instance
{
get
{
return instance;
}
}
public void Debug(string logMessage)
{
message = PrepareLog(logMessage);
ILogger.Debug(message);
}
protected string PrepareLog(string logMessage)
{
string message = GetFileMethodLineNumberInfo();
message += logMessage;
return message;
}
protected string GetFileMethodLineNumberInfo()
{
StackTrace stackTrace = new StackTrace(true);
// The position 3 is relative to the index of the specified method
StackFrame stackFrame = stackTrace.GetFrame(3);
return (stackFrame.GetMethod().DeclaringType.Name
+ "/" + stackFrame.GetMethod().Name
+ "/" + stackFrame.GetFileLineNumber()
+ ":");
}
private static void SetLevel(string loggerName, string levelName)
{
ILogger = LogManager.GetLogger(loggerName);
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
l.Level = l.Hierarchy.LevelMap[levelName];
}
private static void AddAppender(string loggerName, IAppender appender)
{
ILogger = LogManager.GetLogger(loggerName);
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
l.AddAppender(appender);
}
private static IAppender CreateFileAppender(string name, string fileName)
{
FileAppender appender = new FileAppender();
appender.Name = name;
appender.File = fileName;
appender.AppendToFile = true;
//PatternLayout layout = new PatternLayout();
//layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
//layout.ActivateOptions();
//appender.Layout = layout;
appender.ActivateOptions();
return appender;
}
}
}
my application runs on a customized client framework, the client framework uses log4net to log their own log files. We are(our application) has to use the same log4net to log our log files in our own path(say our customized path). currently the our log files are created but log are not writing in that file. It is writing in the client framework log file.
I searched lot of sites the link
Log4Net: Programmatically specify multiple loggers (with multiple file appenders)
helped me to configure the log4net config programmatically,
still my log statements are not written in my log file.the code used as below
public class TraceLog
{
private string message = string.Empty;
private static ILog ILogger = null;
private static TraceLog instance = new TraceLog();
private TraceLog()
{
SetLevel("Log4net.MainForm", "ALL");
AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender", "C:\\mylog.log"));
}
public static TraceLog Instance
{
get
{
return instance;
}
}
public void Debug(string logMessage)
{
message = PrepareLog(logMessage);
ILogger.Debug(message);
}
protected string PrepareLog(string logMessage)
{
string message = GetFileMethodLineNumberInfo();
message += logMessage;
return message;
}
protected string GetFileMethodLineNumberInfo()
{
StackTrace stackTrace = new StackTrace(true);
// The position 3 is relative to the index of the specified method
StackFrame stackFrame = stackTrace.GetFrame(3);
return (stackFrame.GetMethod().DeclaringType.Name
+ "/" + stackFrame.GetMethod().Name
+ "/" + stackFrame.GetFileLineNumber()
+ ":");
}
private static void SetLevel(string loggerName, string levelName)
{
ILogger = LogManager.GetLogger(loggerName);
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
l.Level = l.Hierarchy.LevelMap[levelName];
}
private static void AddAppender(string loggerName, IAppender appender)
{
ILogger = LogManager.GetLogger(loggerName);
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
l.AddAppender(appender);
}
private static IAppender CreateFileAppender(string name, string fileName)
{
FileAppender appender = new FileAppender();
appender.Name = name;
appender.File = fileName;
appender.AppendToFile = true;
//PatternLayout layout = new PatternLayout();
//layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
//layout.ActivateOptions();
//appender.Layout = layout;
appender.ActivateOptions();
return appender;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试调试 log4net:
如何追踪 log4net 问题
try debugging log4net:
How to track down log4net problems
我对这个问题的解决方案:
My resolve for this problem: