I have an executable that depending on the command line switch supplied looks something like:
我有一个可执行文件,根据提供的命令行开关看起来像:
Program.cs -
namespace DiskSpaceReporting
{
class Program
{
static void Main(string[] args)
{
if(args.Length == 1)
{
switch(args[0])
{
case "-summarytotals":
SummaryDiskSpaceReporter.Run();
break;
case "-detailed":
DetailedDiskSpaceReporter.Run();
break;
//...other reporting types
}
}
}
}
}
SummaryDiskSpaceReporter.cs
namespace DiskSpaceReporting
{
public class SummaryDiskSpaceReporter
{
private static IEventIDLog log = EventIDLogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Run()
{
log.Info(1234, "Starting");
//...do work
string message = Helpers.CreateMessage(messageID);
//...do work
}
}
}
DetailedDiskSpaceReporter.cs
namespace DiskSpaceReporting
{
public class DetailedDiskSpaceReporter
{
private static IEventIDLog log = EventIDLogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Run()
{
log.Info(1234, "Starting");
//...do work
string message = Helpers.CreateMessage(messageID);
//...do work
}
}
}
Helpers.cs
namespace DiskSpaceReporting
{
public class Helpers
{
private static IEventIDLog log = ???
public static string CreateMessage(Guid messageID)
{
log.Info(9876, "Starting");
//...do work
}
}
}
In my log4net config I have two separate loggers, one for each of the SummaryDiskSpaceReporter and DetailedDiskSpaceReporter because their logging requirements are different:
在我的log4net配置中,我有两个独立的记录器,每个记录器对应于SummaryDiskSpaceReporter和DetailedDiskSpaceReporter,因为它们的记录要求不同:
<root>
<level value="ALL" />
<appender-ref ref="ConsoleLogAppender" />
<appender-ref ref="EventLogAppender" />
</root>
<logger name="DiskSpaceReporting.SummaryDiskSpaceReporter">
<appender-ref ref="SummaryDiskSpaceReporterRollingFileAppender"/>
</logger>
<logger name="DiskSpaceReporting.DetailedDiskSpaceReporter">
<appender-ref ref="DetailedDiskSpaceReporterRollingFileAppender"/>
</logger>
Both SummaryDiskSpaceReporter and DetailedDiskSpaceReporter call a helper method in a class called Helpers. I want to put some logging into the helper class methods.
SummaryDiskSpaceReporter和DetailedDiskSpaceReporter都在名为Helpers的类中调用辅助方法。我想在helper类方法中加入一些日志。
So...the question is, how do I get the Helpers.CreateMessage() method to use the same logger as its caller?
所以...问题是,如何让Helpers.CreateMessage()方法使用与调用者相同的记录器?
i.e.
SummaryDiskSpaceReporter.Run() -> use DiskSpaceReporting.SummaryDiskSpaceReporter logger DetailedDiskSpaceReporter.Run() -> use DiskSpaceReporting.DetailedDiskSpaceReporter logger.
SummaryDiskSpaceReporter.Run() - >使用DiskSpaceReporting.SummaryDiskSpaceReporter记录器DetailedDiskSpaceReporter.Run() - >使用DiskSpaceReporting.DetailedDiskSpaceReporter记录器。
Cheers Kev
1 个解决方案
#1
2
I have been playing with this question for quite some time and unfortunately have to give up for a while and return to doing "real" work. For the moment what I have come up with is as follows:
我一直在玩这个问题很长一段时间,不幸的是我不得不放弃一段时间并回到做“真正的”工作。目前我提出的内容如下:
Options 1: Pass the logger in
This is not a great option, but should work fine if you do not mind coupling your helper to a logger. Simply allow your CreateMessage method to take a IEventIDLog. You can then use this to log to the correct passed in logger.
选项1:传递记录器这不是一个很好的选择,但如果您不介意将助手连接到记录器,则应该可以正常工作。只需允许您的CreateMessage方法获取IEventIDLog。然后,您可以使用它来记录正确的传入记录器。
public static string CreateMessage(Guid messageID, IEventIDLog log)
{
log.Info(9876, "Starting");
//...do work
}
Not exactly shiny, but it should work!
不完全闪亮,但它应该工作!
Options 2: Use the call stack
Make use of the call stack to find the calling code and from this find the type and get the logger you want from that type
选项2:使用调用堆栈使用调用堆栈查找调用代码,然后从中查找类型并从该类型获取所需的记录器
public static string CreateMessage(Guid messageID)
{
StackFrame frame = new StackTrace().GetFrame(1);
IEventIDLog log = EventIDLogManager.GetLogger(frame.GetMethod().DeclaringType);
log.Info(9876, "Starting");
//...do work
}
Still not shiny since we need to touch the call stack and what happens if the calling code does not have a logger.
因为我们需要触摸调用堆栈而且如果调用代码没有记录器会发生什么,所以仍然没有闪亮。
Option 3: use log4net
This is the option we want to use, hell it is what the question wants as its answer, but I have not yet worked it out yet. :) Looking through log4net we find cool things like the Hierarchy class that allows us to get the root logger.
选项3:使用log4net这是我们想要使用的选项,问题就是它的答案,但我尚未解决它。 :)通过log4net,我们发现很酷的东西,比如Hierarchy类,它允许我们获取根记录器。
Logger logger1 = ((log4net.Repository.Hierarchy.Hierarchy) log4net.LogManager.GetRepository()).Root;
So I am sure there is a way to get the logger one level up from CreateMessage method, now just to find it. :)
所以我确信有一种方法可以将记录器从CreateMessage方法中提升一级,现在只是为了找到它。 :)
#1
2
I have been playing with this question for quite some time and unfortunately have to give up for a while and return to doing "real" work. For the moment what I have come up with is as follows:
我一直在玩这个问题很长一段时间,不幸的是我不得不放弃一段时间并回到做“真正的”工作。目前我提出的内容如下:
Options 1: Pass the logger in
This is not a great option, but should work fine if you do not mind coupling your helper to a logger. Simply allow your CreateMessage method to take a IEventIDLog. You can then use this to log to the correct passed in logger.
选项1:传递记录器这不是一个很好的选择,但如果您不介意将助手连接到记录器,则应该可以正常工作。只需允许您的CreateMessage方法获取IEventIDLog。然后,您可以使用它来记录正确的传入记录器。
public static string CreateMessage(Guid messageID, IEventIDLog log)
{
log.Info(9876, "Starting");
//...do work
}
Not exactly shiny, but it should work!
不完全闪亮,但它应该工作!
Options 2: Use the call stack
Make use of the call stack to find the calling code and from this find the type and get the logger you want from that type
选项2:使用调用堆栈使用调用堆栈查找调用代码,然后从中查找类型并从该类型获取所需的记录器
public static string CreateMessage(Guid messageID)
{
StackFrame frame = new StackTrace().GetFrame(1);
IEventIDLog log = EventIDLogManager.GetLogger(frame.GetMethod().DeclaringType);
log.Info(9876, "Starting");
//...do work
}
Still not shiny since we need to touch the call stack and what happens if the calling code does not have a logger.
因为我们需要触摸调用堆栈而且如果调用代码没有记录器会发生什么,所以仍然没有闪亮。
Option 3: use log4net
This is the option we want to use, hell it is what the question wants as its answer, but I have not yet worked it out yet. :) Looking through log4net we find cool things like the Hierarchy class that allows us to get the root logger.
选项3:使用log4net这是我们想要使用的选项,问题就是它的答案,但我尚未解决它。 :)通过log4net,我们发现很酷的东西,比如Hierarchy类,它允许我们获取根记录器。
Logger logger1 = ((log4net.Repository.Hierarchy.Hierarchy) log4net.LogManager.GetRepository()).Root;
So I am sure there is a way to get the logger one level up from CreateMessage method, now just to find it. :)
所以我确信有一种方法可以将记录器从CreateMessage方法中提升一级,现在只是为了找到它。 :)