【问题标题】:Can I use TraceSource in ASP .Net Core for logging?我可以在 ASP .Net Core 中使用 TraceSource 进行日志记录吗?
【发布时间】:2019-04-23 14:51:33
【问题描述】:

可以从我的 ASP .NET Core 项目中的 System.Diagnostics 访问 TraceSource。

在 src 文件中你可以找到 header:

#region Assembly System.Diagnostics.TraceSource, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.Diagnostics.TraceSource.dll
#endregion

这是什么意思? .Net Famework >=4.1.1.0 的版本是否可以接受? TraceSource 是否包含在某些版本的 .Net Standard 中?

更新我的解决方案: 需要配置。

1) app.config 仅适用于 .NET Framework,https://github.com/dotnet/corefx/issues/24829

2) .Net Core 草案:

TraceSource.Listeners.Add(new MyListener());
TraceSource.Switch = new SourceSwitch();

【问题讨论】:

  • 跟踪源出现在异常处理程序 try/catch 中。您应该有以下内容: Catch(Exception e) { Console.WriteLine(e.TraceSource); }

标签: c# .net .net-core .net-standard tracesource


【解决方案1】:

这个 sn-p 可以帮到你。

public static void Main(string[] args)
{
    var webHost = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", 
                      optional: true, reloadOnChange: true);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((hostingContext, logging) =>
        {

          logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
             logging.AddConsole();
             logging.AddDebug();
             logging.AddEventSourceLogger();
        })
        .UseStartup<Startup>()
        .Build();

    webHost.Run();
}

您也可以关注this link,获取有关登录 dotnet core 的深入指南。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
  • 2010-10-15
  • 2018-12-09
相关资源
最近更新 更多