- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有什么方法可以获取上下文,以便检索 loggerName
并使用 LogManager.GetLogger(loggerName)
而不是 LogManager.GetCurrentClassLogger()
?
我注意到 container.RegisterConditional()
可以访问上下文。
另外,我想避免像 SimpleLogging.NLog 这样的解决方案目前。
最后,我愿意接受这不是正确的做法。顺便说一句,AOP 是我已经探索过的一个选项 (Is it a good practice to have logger as a singleton?)。
注意:我知道 GetCurrentClassLogger()
获得与 .NET 反射相同的信息。
using NLog;
using SimpleInjector;
namespace DependencyInjection
{
class Program
{
private static Container _container;
static void Main(string[] args)
{
Bootstrap();
_container.GetInstance<Greeter>().Greet();
}
private static void Bootstrap()
{
_container = new Container();
_container.Register<ILogger>(() => LogManager.GetCurrentClassLogger(), Lifestyle.Transient);
_container.Register<Greeter>();
_container.Verify();
}
public class Greeter
{
private ILogger _logger;
public Greeter(ILogger logger)
{
_logger = logger;
}
public void Greet()
{
_logger.Log(LogLevel.Info, "Hello world!");
}
}
}
}
最佳答案
您需要定义一个代理记录器,它将消息路由到正确的 Nlog 记录器。这个代理很简单:
public class NLogProxy<T> : ILogger
{
private static readonly NLog.ILogger logger =
LogManager.GetLogger(typeof (T).FullName);
void ILogger.Log(string message)
{
logger.Log(LogLevel.Info, message);
}
}
container.RegisterConditional(typeof(ILogger),
context => typeof(NLogProxy<>).MakeGenericType(context.Consumer.ImplementationType),
Lifestyle.Singleton, context => true);
ILogger
.
A wrapper that has to be maintained (NLog.ILogger contract is huge).
public interface ISomeService
{
void DoSomething(string someParameter);
}
public class SomeServiceDecorator : ISomeService
{
private readonly ISomeService decoratee;
private readonly ILogger logger;
public SomeServiceDecorator(ISomeService decoratee, ILogger logger)
{
this.decoratee = decoratee;
this.logger = logger;
}
public void DoSomething(string someParameter)
{
try
{
this.logger.Log(string.Format("Do something called with {0}", someParameter));
this.decoratee.DoSomething(someParameter);
}
catch (Exception e)
{
this.logger.Log(e.ToString());
throw;
}
}
}
public class LoggingCommandHandlerDecorator<T> : ICommandHandler<T>
{
private readonly ICommandHandler<T> decoratee;
private readonly ILogger logger;
public LoggingCommandHandlerDecorator(ICommandHandler<T> decoratee, ILogger logger)
{
this.decoratee = decoratee;
this.logger = logger;
}
public void Handle(T command)
{
// serialize command to json and log
this.logger.Log(serializedcommandData);
this.decoratee.Handle(command);
}
}
关于.net - 使用 Simple Injector 注册 NLog ILogger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643533/
我想创建一个对象,比方说一个饼图。 class Pie def initialize(name, flavor) @name = name @flavor = flavor e
我正在寻找类似于 Log property in Linq DataContext 的功能. 最佳答案 所有执行的 SQL 都写入 Trace,因此您可以使用 TraceListener 访问。 关于
使用 django-simple-history ,如何从我的模型中获取最后更改的对象? 我尝试了 MyModel.history.most_recent(),它需要一个模型实例,因此可能会返回所选实
我定期使用 Eclipse 调试器,这一直困扰着我关于 IDE 的问题。步进过滤是一个非常宝贵的工具,这样我就不会进入没有源代码或者我根本不感兴趣的类。但是,Eclipse 在大多数情况下都没有正确处
我基于有向图构建一棵树。源数据是 SQL 表中的一系列父子关系。它肯定是一棵树(无论如何我都会验证)。我想要一组从根到每片叶子的简单路径。数据是会计“科目表”中的标题,路径类似于“根 -> Asset
我对如何在torii中使用ember-simple-auth有点困惑 我正在使用ember-cli-simple-auth和ember-cli-simple-auth-torii 我得到下面提到的错误
考虑以下用 simple-xml 注释注释的枚举: @Root(name="days") public enum DaysOfWeek { SUNDAY("blue", 30), MO
我有一个 json 文件,我正在 excel 中创建一些 vba 代码。我想用java读取这个json文件。为此,我编写了以下代码 try { Class.forName("org.j
我在尝试使用 google 的 simple-json 解析一个简单的 json 时遇到了一个奇怪的问题。 这是我的代码,它不起作用: String s = args[0].toString(); J
我正在尝试解析下面的 json 文件: {"units":[{"id":42, "title":"Hello World", "positi
我正在使用 simple-import-sort eslint 插件进行 react 。我想我的 .eslintrc.js是对的,但我无法使这个特定的插件工作。我在文件的第一行收到以下错误: 未找到规
我正在尝试解析子文件,但我不知道自己做错了什么(当然,我也不知道自己做对了什么)。 文件.json [{ "arrOne":{ "one":"a", "two":"b",
在我的Angel 16(独立的)应用程序中,我有一个简单的服务来测试LangChain和OpenAI。同样的代码在NodeJS应用程序中运行得非常好,然而,当从角度运行时,OpenAI返回的结果总是空
我正在尝试测试连接到数据库的 Web 应用程序 - 我的 Junit 代码不执行连接。 设置按照 https://github.com/h-thurow/Simple-JNDI InitialCont
我正在尝试制作一个小型应用程序,它可以通过 ISBN 在亚马逊上搜索一本书。我是 Amazon Web Service 的新手。 我正在关注以下链接: http://flyingpies.wordpr
我正在使用简单 XML 序列化 (simple-xml-2.6.6.jar) here将我的 XML 响应从 webservice 转换为 POJO 类。 XML 是:
Simple UI混合开发的必经之路~ Simple UI快速上手 在混合开发的模式下,如果想使用django admin,又嫌弃后台不符合你的审美?Simple UI给你想要的答案,我不是打广告的~
Simple JSON是Google开发的Java JSON解析框架,基于Apache协议。 下载的文件是:json_simple.jar 例子1:很方便的方式,使用JSONValue
我有一个 simple_form 表单设置,它会很好地显示内联错误。我遇到过一些用户看不到这些错误的问题,并要求在非常长的表格顶部进行清晰的枚举。我使用了 Rails 教程中的代码设置:
我正在使用简单的 XML 框架,只是重命名了一些 XML 布局,这些布局现在似乎不再起作用了。 这是我的 XML: 2 0 1
我是一名优秀的程序员,十分优秀!