C# 将ILogger注入.NETCore3中Thrift调用的函数中
我想知道如何将ILogger注入ASP.NET核心应用程序中的函数中,该应用程序由Java客户端通过Thrift调用 下面是我想做的高级代码演示:C# 将ILogger注入.NETCore3中Thrift调用的函数中,c#,asp.net-core,.net-core,thrift,C#,Asp.net Core,.net Core,Thrift,我想知道如何将ILogger注入ASP.NET核心应用程序中的函数中,该应用程序由Java客户端通过Thrift调用 下面是我想做的高级代码演示: // ExecuteRequest is called by java client through Thrift public class ThriftLayer { ... public string ExecuteRequest(...params) { ... var result =
// ExecuteRequest is called by java client through Thrift
public class ThriftLayer
{
...
public string ExecuteRequest(...params)
{
...
var result = RequestFunc1(...params);
...do processing
return result;
}
...
}
// Contains request functions called by ExecuteRequest
public class ServerRequestHandler
{
...
public string RequestFunc1(...params)
{
return TaskFunc1(...params);
}
....
}
// Functions in this class are called by both the Thrift layer(called by ServerRequestHandler) as well as a Console application
// In Console applications, we can inject the ILogger at Startup - No issues there.
public class TaskFunctions
{
private readonly ILogger<TaskFunctions> _logger;
public TaskFunctions(ILogger<TaskFunctions> logger)
{
_logger = logger;
}
public string TaskFunc1(...params)
{
_logger.logInfo("<log message>");
...do processing
return stringResult;
}
}
//java客户端通过Thrift调用ExecuteRequest
公营节俭者
{
...
公共字符串ExecuteRequest(…参数)
{
...
var result=RequestFunc1(…参数);
…进行处理
返回结果;
}
...
}
//包含ExecuteRequest调用的请求函数
公共类ServerRequestHandler
{
...
公共字符串RequestFunc1(…参数)
{
返回TaskFunc1(…参数);
}
....
}
//此类中的函数由Thrift层(由ServerRequestHandler调用)和控制台应用程序调用
//在控制台应用程序中,我们可以在启动时注入ILogger—没有问题。
公共类任务函数
{
专用只读ILogger\u记录器;
公共任务功能(ILogger记录器)
{
_记录器=记录器;
}
公共字符串TaskFunc1(…参数)
{
_logger.logInfo(“”);
…进行处理
返回结果;
}
}
所以我想知道如何在从Thrift调用时将ILogger注入TaskFunction 这个关于StackOverflow的问题将对您有所帮助
您需要在ASP.NET核心的启动
类之外构建一个ServiceCollection
- 应用程序的MVC部分将在
方法中添加这些服务Startup.ConfigureServices
- 应用程序的另一部分将需要构建并使用服务提供程序,例如
var taskFunctionsWithInjected=ActivatorUtilities.CreateInstance(serviceProvider)代码>以获取依赖项