How do I add cross-cutting information to Microsoft.Extensions.Logging events?











up vote
0
down vote

favorite












I'm trying to set up a basic prototype of client-server applications, both using ASP.NET Core 2.0. As part of this, I'm sending diagnostic information to Seq. In the Startup class for each application, I'm using Seq.Extensions.Logging and configuring the Seq sinks as follows:



public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder =>
{
builder
.SetMinimumLevel(LogLevel.Debug)
.AddSeq();
});


This works; all the events appear in Seq. However, it's not possible to easily distinguish which application any specific event is from.



In the past, using Serilog, I have been able to add propertes to the logger which are included in every event. Typically I would add the name of the application and the host it is running on as a pair of additional properties:



Log.Logger = new LoggerConfiguration()
.Enrich.WithMachineName()
// ...other configuration...
.CreateLogger();


Is there a way to configure the Microsoft logging extensions to do the same thing?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to set up a basic prototype of client-server applications, both using ASP.NET Core 2.0. As part of this, I'm sending diagnostic information to Seq. In the Startup class for each application, I'm using Seq.Extensions.Logging and configuring the Seq sinks as follows:



    public void ConfigureServices(IServiceCollection services)
    {
    services.AddLogging(builder =>
    {
    builder
    .SetMinimumLevel(LogLevel.Debug)
    .AddSeq();
    });


    This works; all the events appear in Seq. However, it's not possible to easily distinguish which application any specific event is from.



    In the past, using Serilog, I have been able to add propertes to the logger which are included in every event. Typically I would add the name of the application and the host it is running on as a pair of additional properties:



    Log.Logger = new LoggerConfiguration()
    .Enrich.WithMachineName()
    // ...other configuration...
    .CreateLogger();


    Is there a way to configure the Microsoft logging extensions to do the same thing?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to set up a basic prototype of client-server applications, both using ASP.NET Core 2.0. As part of this, I'm sending diagnostic information to Seq. In the Startup class for each application, I'm using Seq.Extensions.Logging and configuring the Seq sinks as follows:



      public void ConfigureServices(IServiceCollection services)
      {
      services.AddLogging(builder =>
      {
      builder
      .SetMinimumLevel(LogLevel.Debug)
      .AddSeq();
      });


      This works; all the events appear in Seq. However, it's not possible to easily distinguish which application any specific event is from.



      In the past, using Serilog, I have been able to add propertes to the logger which are included in every event. Typically I would add the name of the application and the host it is running on as a pair of additional properties:



      Log.Logger = new LoggerConfiguration()
      .Enrich.WithMachineName()
      // ...other configuration...
      .CreateLogger();


      Is there a way to configure the Microsoft logging extensions to do the same thing?










      share|improve this question













      I'm trying to set up a basic prototype of client-server applications, both using ASP.NET Core 2.0. As part of this, I'm sending diagnostic information to Seq. In the Startup class for each application, I'm using Seq.Extensions.Logging and configuring the Seq sinks as follows:



      public void ConfigureServices(IServiceCollection services)
      {
      services.AddLogging(builder =>
      {
      builder
      .SetMinimumLevel(LogLevel.Debug)
      .AddSeq();
      });


      This works; all the events appear in Seq. However, it's not possible to easily distinguish which application any specific event is from.



      In the past, using Serilog, I have been able to add propertes to the logger which are included in every event. Typically I would add the name of the application and the host it is running on as a pair of additional properties:



      Log.Logger = new LoggerConfiguration()
      .Enrich.WithMachineName()
      // ...other configuration...
      .CreateLogger();


      Is there a way to configure the Microsoft logging extensions to do the same thing?







      c# logging asp.net-core-2.0 seq






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Paul Turner

      24.8k1179139




      24.8k1179139
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          There are two options for you.





          1. Use built-in Seq feature.



            For Seq, it provides API Keys which is used to identity the client application. You could add the Applied properties with the specific name like your project name, and configure the api key by



                    services.AddLogging(loggingBuilder =>
            {
            loggingBuilder.AddSeq(Configuration.GetSection("Seq"));
            });


            Or



                    services.AddLogging(loggingBuilder =>
            {
            loggingBuilder.AddSeq("http://xxx:5341", apiKey: "K5HzACIfiQxr67CKlvUj");
            });



          2. Use Serialog to configure the output template like



                    var output = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {ActionName} {UserName} {NewLine}{Exception} {MachineName}";

            Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext() // Populates a 'User' property on every log entry
            .Enrich.WithProperty("MachineName", Environment.MachineName) //new field
            .WriteTo.RollingFile("Logs/app-{Date}.txt", outputTemplate: output)
            .WriteTo.Seq("http://xxx:5341",apiKey: "K5HzACIfiQxr67CKlvUj")
            .CreateLogger();
            loggerFactory
            //.AddFile("Logs/app-{Date}.txt")
            .AddSerilog();







          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409133%2fhow-do-i-add-cross-cutting-information-to-microsoft-extensions-logging-events%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            There are two options for you.





            1. Use built-in Seq feature.



              For Seq, it provides API Keys which is used to identity the client application. You could add the Applied properties with the specific name like your project name, and configure the api key by



                      services.AddLogging(loggingBuilder =>
              {
              loggingBuilder.AddSeq(Configuration.GetSection("Seq"));
              });


              Or



                      services.AddLogging(loggingBuilder =>
              {
              loggingBuilder.AddSeq("http://xxx:5341", apiKey: "K5HzACIfiQxr67CKlvUj");
              });



            2. Use Serialog to configure the output template like



                      var output = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {ActionName} {UserName} {NewLine}{Exception} {MachineName}";

              Log.Logger = new LoggerConfiguration()
              .Enrich.FromLogContext() // Populates a 'User' property on every log entry
              .Enrich.WithProperty("MachineName", Environment.MachineName) //new field
              .WriteTo.RollingFile("Logs/app-{Date}.txt", outputTemplate: output)
              .WriteTo.Seq("http://xxx:5341",apiKey: "K5HzACIfiQxr67CKlvUj")
              .CreateLogger();
              loggerFactory
              //.AddFile("Logs/app-{Date}.txt")
              .AddSerilog();







            share|improve this answer

























              up vote
              0
              down vote













              There are two options for you.





              1. Use built-in Seq feature.



                For Seq, it provides API Keys which is used to identity the client application. You could add the Applied properties with the specific name like your project name, and configure the api key by



                        services.AddLogging(loggingBuilder =>
                {
                loggingBuilder.AddSeq(Configuration.GetSection("Seq"));
                });


                Or



                        services.AddLogging(loggingBuilder =>
                {
                loggingBuilder.AddSeq("http://xxx:5341", apiKey: "K5HzACIfiQxr67CKlvUj");
                });



              2. Use Serialog to configure the output template like



                        var output = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {ActionName} {UserName} {NewLine}{Exception} {MachineName}";

                Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext() // Populates a 'User' property on every log entry
                .Enrich.WithProperty("MachineName", Environment.MachineName) //new field
                .WriteTo.RollingFile("Logs/app-{Date}.txt", outputTemplate: output)
                .WriteTo.Seq("http://xxx:5341",apiKey: "K5HzACIfiQxr67CKlvUj")
                .CreateLogger();
                loggerFactory
                //.AddFile("Logs/app-{Date}.txt")
                .AddSerilog();







              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                There are two options for you.





                1. Use built-in Seq feature.



                  For Seq, it provides API Keys which is used to identity the client application. You could add the Applied properties with the specific name like your project name, and configure the api key by



                          services.AddLogging(loggingBuilder =>
                  {
                  loggingBuilder.AddSeq(Configuration.GetSection("Seq"));
                  });


                  Or



                          services.AddLogging(loggingBuilder =>
                  {
                  loggingBuilder.AddSeq("http://xxx:5341", apiKey: "K5HzACIfiQxr67CKlvUj");
                  });



                2. Use Serialog to configure the output template like



                          var output = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {ActionName} {UserName} {NewLine}{Exception} {MachineName}";

                  Log.Logger = new LoggerConfiguration()
                  .Enrich.FromLogContext() // Populates a 'User' property on every log entry
                  .Enrich.WithProperty("MachineName", Environment.MachineName) //new field
                  .WriteTo.RollingFile("Logs/app-{Date}.txt", outputTemplate: output)
                  .WriteTo.Seq("http://xxx:5341",apiKey: "K5HzACIfiQxr67CKlvUj")
                  .CreateLogger();
                  loggerFactory
                  //.AddFile("Logs/app-{Date}.txt")
                  .AddSerilog();







                share|improve this answer












                There are two options for you.





                1. Use built-in Seq feature.



                  For Seq, it provides API Keys which is used to identity the client application. You could add the Applied properties with the specific name like your project name, and configure the api key by



                          services.AddLogging(loggingBuilder =>
                  {
                  loggingBuilder.AddSeq(Configuration.GetSection("Seq"));
                  });


                  Or



                          services.AddLogging(loggingBuilder =>
                  {
                  loggingBuilder.AddSeq("http://xxx:5341", apiKey: "K5HzACIfiQxr67CKlvUj");
                  });



                2. Use Serialog to configure the output template like



                          var output = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {ActionName} {UserName} {NewLine}{Exception} {MachineName}";

                  Log.Logger = new LoggerConfiguration()
                  .Enrich.FromLogContext() // Populates a 'User' property on every log entry
                  .Enrich.WithProperty("MachineName", Environment.MachineName) //new field
                  .WriteTo.RollingFile("Logs/app-{Date}.txt", outputTemplate: output)
                  .WriteTo.Seq("http://xxx:5341",apiKey: "K5HzACIfiQxr67CKlvUj")
                  .CreateLogger();
                  loggerFactory
                  //.AddFile("Logs/app-{Date}.txt")
                  .AddSerilog();








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 23 hours ago









                Tao Zhou

                3,77721026




                3,77721026






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409133%2fhow-do-i-add-cross-cutting-information-to-microsoft-extensions-logging-events%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Lallio

                    Futebolista

                    Jornalista