登录没有.DI的.NET Core? [英] Logging in .NET Core without DI?

查看:55
本文介绍了登录没有.DI的.NET Core?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎Microsoft确实在尝试使用.NET Core来简化DI,我不确定为什么,但是坦率地说,我的控制台应用程序又小又简单,我只是不想构建整个DI容器只是做一些简单的日志记录.如何在不使用DI的情况下登录.NET Core?我读过的所有内容都假定您将使用.NET Core的内置日志记录体系结构,该体系结构显然需要DI,但是必须有一种方法可以在类上使用静态变量而无需DI来做到这一点?

It seems that Microsoft are really trying to shove DI down your throat with .NET Core, and I'm not sure why, but frankly my console app is small and simple and I just don't want to build a whole DI container just to do some simple logging. How can I do logging in .NET Core without using DI? Everything I've read assumed you're going to use .NET Core's built-in logging architecture which obviously requires DI, but there must be a way to just do it without DI using a static variable on the class?

推荐答案

我最终要做的是使用NLog,而不是使用他们的.NET Core DI扩展,我只添加了

What I ended up doing was using NLog but rather than using their .NET Core DI extensions, I just included the main NLog NuGet package, manually creating my own NLog.config file, and followed the tutorial to GetCurrentClassLogger(), creating that as a static member of my class, then directly used that for logging. This is quite a simple setup and has no need for DI. NLog should probably document it better.

这篇关于登录没有.DI的.NET Core?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆