c# - 我可以在 ASP .Net Core 中使用 TraceSource 进行日志记录吗?

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

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

在 src 文件中你可以找到标题:

#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();

最佳答案

此代码段可能会帮助您。

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 核心的深入指南。

关于c# - 我可以在 ASP .Net Core 中使用 TraceSource 进行日志记录吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55813908/

相关文章:

c# - 在 C# 中连接到外部 SQL 数据库

c# - 通过反射在复制的列表中设置属性更新原始列表

javascript - 将 JavaScript 组件包含到 Blazor 组件中

c# - 将 Rx 与 Prism 的 EventAggregator 一起使用或与其一起使用 - 建议的方法?

visual-studio - 如果生成单个文件,如何在堆栈跟踪中显示行号

.net - 如何使用.Net Core 1.0开发Android应用程序?

json - .net Core 控制台应用程序中的 appsettings.json

c# - 如何打印每个子目录,但是每个子目录都应该在打印之前被选中

c# - 当前上下文中不存在 TextToSplit

c# - 我该如何修复 csharp-mode.el?