asp.net核心webapi中的log4net实施

问题描述 投票:0回答:1

我有一个asp.net核心网络api。到目前为止,我正在使用ILogger记录消息。但是ILogger中没有致命日志级别,虽然有严重级别,但我们的团队需要致命单词而不是关键单词,我有什么办法可以将打印到日志的工作进行整周处理?

如果不是,我想用具有致命级别的log4Net替换ILogger。所以这是我所做的,但是不起作用。我有多层体系结构:ApiServiceApiService.Helper。所有这些都是解决方案中包含的不同项目。

ApiService中:我添加了Microsoft.Extensions.Logging.Log4Net.AspNetCore参考。在startup.cs

public void ConfigureServices(IServiceCollection apiServices)
        {
            var provider = apiServices.BuildServiceProvider();

            var factory = new LoggerFactory()
                   .AddConsole().AddApplicationInsights(provider, LogLevel.Information);

            apiServices.AddSingleton(factory);
            apiServices.AddLogging();

            apiServices.AddMvc();
            apiServices.AddOptions();
            apiServices.AddSingleton<ILog4NetHelper, Log4NetHelper>();
        }

HomeController.cs

[ApiController]
    public class HomeController : Controller
    {
        private readonly ILog4NetHelper _logHelper;
        public HomeController(ILog4NetHelper logHelper)
        {
            _logHelper = logHelper;
        }
        [HttpGet]
        [Route("/home")]
        public virtual IActionResult GetData()
        {
            try
            {
                _logHelper.Log4NetMessage("Fatal", "Start Getdata ");
                return new OkObjectResult("Your in Home Controller");
            }
            catch (Exception ex)
            {
                _logHelper.Log4NetMessage("Error", "Exception in getData" + ex.Message);
                throw;
            }
        }
    }

并且在ApiService.Helper项目中,我添加了接口ILog4NetHelper和实现该接口Log4NetHelper的类。另外我还添加了log4Net配置文件。

public class Log4NetHelper : ILog4NetHelper
    {
        readonly ILog _log = log4net.LogManager.GetLogger(typeof(Log4NetHelper));

        public Log4NetHelper(Log4NetHelper log)
        {
            _log = (ILog)log;
        }
        public void Log4NetMessage(string type,string message)
        {
            string logMessage = message;

            switch (type)
            {
                case "Info":
                    _log.Info(logMessage);
                    break;
                case "Error":
                    _log.Error(logMessage);
                    break;
                case "Fatal":
                    _log.Fatal(logMessage);
                    break;
                default:
                    _log.Info(logMessage);
                    break;
            }
        }
    }

当我托管该应用程序并运行它时,它给了我500内部服务器错误。有人可以指出我在做什么错吗?非常感谢。

c# log4net asp.net-core-webapi
1个回答
0
投票

[当您混合事物时,我没有完全得到它。

尝试使用下面的Log4Helper类代码。我不确定,但请检查是否可行。

public class Log4NetHelper : ILog4NetHelper
{
    readonly ILog _log;

    public Log4NetHelper()
    {
        _log = log4net.LogManager.GetLogger(typeof(Log4NetHelper));
    }
    public void Log4NetMessage(string type,string message)
    {
        string logMessage = message;

        switch (type)
        {
            case "Info":
                _log.Info(logMessage);
                break;
            case "Error":
                _log.Error(logMessage);
                break;
            case "Fatal":
                _log.Fatal(logMessage);
                break;
            default:
                _log.Info(logMessage);
                break;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.