Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 非托管.NET控制台中的依赖项注入日志记录_C#_Logging_.net Core_Dependency Injection_.net Core Logging - Fatal编程技术网

C# 非托管.NET控制台中的依赖项注入日志记录

C# 非托管.NET控制台中的依赖项注入日志记录,c#,logging,.net-core,dependency-injection,.net-core-logging,C#,Logging,.net Core,Dependency Injection,.net Core Logging,如何在非托管控制台应用程序中实现依赖项注入(如托管应用程序)?在下面的代码中,我希望像logTest1一样工作。可能吗 下面是我的appsettings.json-我希望不必定义每个类,但这无论如何都不起作用 { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "I

如何在非托管控制台应用程序中实现依赖项注入(如托管应用程序)?在下面的代码中,我希望像
logTest1
一样工作。可能吗

下面是我的appsettings.json-我希望不必定义每个类,但这无论如何都不起作用

{
    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "ClientTestCli.LogTest":  "Trace"
      }
}
我的控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        HandleArgs(args);

        var serviceProvider = ContainerConfiguration.Configure();

        var logTest1 = new LogTest();
        var logTest2 = new LogTest(serviceProvider.GetService<ILogger<LogTest>>());
        logTest1.LogStuff();
        logTest2.LogStuff();
    }
}
类程序
{
静态void Main(字符串[]参数)
{
手工学习(args);
var serviceProvider=ContainerConfiguration.Configure();
var logTest1=新的LogTest();
var logTest2=新的日志测试(serviceProvider.GetService());
logTest1.LogStuff();
logTest2.LogStuff();
}
}
容器配置:

internal static class ContainerConfiguration
{
    public static ServiceProvider Configure()
    {
        return new ServiceCollection()
            .AddLogging(l => l.AddConsole())
            .Configure<LoggerFilterOptions>(c => c.MinLevel = LogLevel.Trace)
            .BuildServiceProvider();
    }
}
内部静态类容器配置
{
公共静态ServiceProvider Configure()
{
返回新的ServiceCollection()
.AddLogging(l=>l.AddConsole())
.Configure(c=>c.MinLevel=LogLevel.Trace)
.BuildServiceProvider();
}
}
测试类:

internal class LogTest
{
    ILogger<LogTest> logger;
    public LogTest(ILogger<LogTest> logger = null)
    {
        this.logger = logger;
    }

    public void LogStuff()
    {
        logger?.LogCritical("This is a critical log");
    }
}
内部类日志测试
{
ILogger记录器;
公共日志测试(ILogger logger=null)
{
this.logger=记录器;
}
公众假期
{
记录器?.LogCritical(“这是一个关键日志”);
}
}

LogTest
添加到服务集合中,并使用提供程序解决它,提供程序将注入已配置的记录器

重构你的合成根

internal static class ContainerConfiguration {
    public static IServiceProvider Configure(Action<IServiceCollection> configuration = null) {
        var services = new ServiceCollection()
            .AddLogging(logging => {
                 logging.ClearProviders();
                 logging.AddConsole();
            })
            .Configure<LoggerFilterOptions>(c => c.MinLevel = LogLevel.Trace);

        configuration?.Invoke(services);

        return services.BuildServiceProvider();
    }
}
内部静态类容器配置{
公共静态IServiceProvider配置(操作配置=null){
var services=newservicecolection()
.AddLogging(日志=>{
logging.ClearProviders();
logging.AddConsole();
})
.Configure(c=>c.MinLevel=LogLevel.Trace);
配置?.Invoke(服务);
return services.BuildServiceProvider();
}
}
并在启动时使用它

static void Main(string[] args) {
    HandleArgs(args);

    IServiceProvider serviceProvider = ContainerConfiguration.Configure(services => {
        services.AddTransient<LogTest>();
    });

    LogTest logTest = serviceProvider.GetService<LogTest>();

    logTest.LogStuff();
}
static void Main(字符串[]args){
手工学习(args);
IServiceProvider serviceProvider=ContainerConfiguration.Configure(服务=>{
services.AddTransient();
});
LogTest LogTest=serviceProvider.GetService();
logTest.LogStuff();
}
如果使用提供程序,它将负责构建对象图,其中包括注入所需的依赖项


一个工作示例。

LogTest
添加到服务集合中,并使用提供程序解决它,提供程序将注入已配置的记录器

重构你的合成根

internal static class ContainerConfiguration {
    public static IServiceProvider Configure(Action<IServiceCollection> configuration = null) {
        var services = new ServiceCollection()
            .AddLogging(logging => {
                 logging.ClearProviders();
                 logging.AddConsole();
            })
            .Configure<LoggerFilterOptions>(c => c.MinLevel = LogLevel.Trace);

        configuration?.Invoke(services);

        return services.BuildServiceProvider();
    }
}
内部静态类容器配置{
公共静态IServiceProvider配置(操作配置=null){
var services=newservicecolection()
.AddLogging(日志=>{
logging.ClearProviders();
logging.AddConsole();
})
.Configure(c=>c.MinLevel=LogLevel.Trace);
配置?.Invoke(服务);
return services.BuildServiceProvider();
}
}
并在启动时使用它

static void Main(string[] args) {
    HandleArgs(args);

    IServiceProvider serviceProvider = ContainerConfiguration.Configure(services => {
        services.AddTransient<LogTest>();
    });

    LogTest logTest = serviceProvider.GetService<LogTest>();

    logTest.LogStuff();
}
static void Main(字符串[]args){
手工学习(args);
IServiceProvider serviceProvider=ContainerConfiguration.Configure(服务=>{
services.AddTransient();
});
LogTest LogTest=serviceProvider.GetService();
logTest.LogStuff();
}
如果使用提供程序,它将负责构建对象图,其中包括注入所需的依赖项


这是一个工作示例。

@VicF我刚刚使用运行了它,它按预期执行。我找到了答案。程序在日志写入控制台之前结束。通过在LogTest.LogStuff()调用之后添加一个Thread.Sleep(),它成功了。谢谢你的帮助。@VicF我刚刚用运行了这个程序,它按预期执行。我知道了。程序在日志写入控制台之前结束。通过在LogTest.LogStuff()调用之后添加一个Thread.Sleep(),它成功了。谢谢你的帮助。