Registering log4net named logger .NET Core service provider












0















I am struggling with finding a way to register log4net ILog within IServiceCollection services (.NET Core 2.1).



log4net LogManager provides a method that takes a Type as parameter so that it can be used when logging events. Here is an example call:



var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


However when registering it with services at Startup method, I can use factory resolver, but still the type will be always the same. See the following excerpt:



private void SetServices(IServiceCollection services)
{
services.AddTransient(svc => CreateLog(MethodBase.GetCurrentMethod().DeclaringType)).;
}

protected ILog CreateLog(Type type)
{
return LogManager.GetLogger(type);
}


I was expecting that svc (IServiceProvider) will expose some ways to get to the type being actually resolved but there doesn't seem to be anything.
Also using reflection won't help (IMO, but I could be wrong) because ILog is activated prior to calling the resolved type ctor.



But maybe there is any extension on either MS side or log4net that would help resolving a named logger as explained on the beginning?



This is all to avoid having static members of ILog in any class that uses logging facility and use dependency injection instead.



Thanks, Radek










share|improve this question



























    0















    I am struggling with finding a way to register log4net ILog within IServiceCollection services (.NET Core 2.1).



    log4net LogManager provides a method that takes a Type as parameter so that it can be used when logging events. Here is an example call:



    var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


    However when registering it with services at Startup method, I can use factory resolver, but still the type will be always the same. See the following excerpt:



    private void SetServices(IServiceCollection services)
    {
    services.AddTransient(svc => CreateLog(MethodBase.GetCurrentMethod().DeclaringType)).;
    }

    protected ILog CreateLog(Type type)
    {
    return LogManager.GetLogger(type);
    }


    I was expecting that svc (IServiceProvider) will expose some ways to get to the type being actually resolved but there doesn't seem to be anything.
    Also using reflection won't help (IMO, but I could be wrong) because ILog is activated prior to calling the resolved type ctor.



    But maybe there is any extension on either MS side or log4net that would help resolving a named logger as explained on the beginning?



    This is all to avoid having static members of ILog in any class that uses logging facility and use dependency injection instead.



    Thanks, Radek










    share|improve this question

























      0












      0








      0








      I am struggling with finding a way to register log4net ILog within IServiceCollection services (.NET Core 2.1).



      log4net LogManager provides a method that takes a Type as parameter so that it can be used when logging events. Here is an example call:



      var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


      However when registering it with services at Startup method, I can use factory resolver, but still the type will be always the same. See the following excerpt:



      private void SetServices(IServiceCollection services)
      {
      services.AddTransient(svc => CreateLog(MethodBase.GetCurrentMethod().DeclaringType)).;
      }

      protected ILog CreateLog(Type type)
      {
      return LogManager.GetLogger(type);
      }


      I was expecting that svc (IServiceProvider) will expose some ways to get to the type being actually resolved but there doesn't seem to be anything.
      Also using reflection won't help (IMO, but I could be wrong) because ILog is activated prior to calling the resolved type ctor.



      But maybe there is any extension on either MS side or log4net that would help resolving a named logger as explained on the beginning?



      This is all to avoid having static members of ILog in any class that uses logging facility and use dependency injection instead.



      Thanks, Radek










      share|improve this question














      I am struggling with finding a way to register log4net ILog within IServiceCollection services (.NET Core 2.1).



      log4net LogManager provides a method that takes a Type as parameter so that it can be used when logging events. Here is an example call:



      var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


      However when registering it with services at Startup method, I can use factory resolver, but still the type will be always the same. See the following excerpt:



      private void SetServices(IServiceCollection services)
      {
      services.AddTransient(svc => CreateLog(MethodBase.GetCurrentMethod().DeclaringType)).;
      }

      protected ILog CreateLog(Type type)
      {
      return LogManager.GetLogger(type);
      }


      I was expecting that svc (IServiceProvider) will expose some ways to get to the type being actually resolved but there doesn't seem to be anything.
      Also using reflection won't help (IMO, but I could be wrong) because ILog is activated prior to calling the resolved type ctor.



      But maybe there is any extension on either MS side or log4net that would help resolving a named logger as explained on the beginning?



      This is all to avoid having static members of ILog in any class that uses logging facility and use dependency injection instead.



      Thanks, Radek







      .net-core log4net asp.net-core-2.1 .net-core-2.1






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 12:59









      Radek StrugalskiRadek Strugalski

      1269




      1269
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The way to do it is to implement and then register your own strongly typed Logger class that derives from ILogger.



          .Net Core allows from to register generic type interface implementation with no type specified. In this case it would be:



          services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(MyLogging.Logger<>)));


          This allows all classes that require logging to use constructor injection as follows:



          class A
          {
          public A(Ilogger<A> logger) {...}
          }


          Implementation of ILogger which a wrapper to log4net should be rather simple. Please let me know if you need an example.



          Radek






          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',
            autoActivateHeartbeat: false,
            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%2f53431599%2fregistering-log4net-named-logger-net-core-service-provider%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









            0














            The way to do it is to implement and then register your own strongly typed Logger class that derives from ILogger.



            .Net Core allows from to register generic type interface implementation with no type specified. In this case it would be:



            services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(MyLogging.Logger<>)));


            This allows all classes that require logging to use constructor injection as follows:



            class A
            {
            public A(Ilogger<A> logger) {...}
            }


            Implementation of ILogger which a wrapper to log4net should be rather simple. Please let me know if you need an example.



            Radek






            share|improve this answer




























              0














              The way to do it is to implement and then register your own strongly typed Logger class that derives from ILogger.



              .Net Core allows from to register generic type interface implementation with no type specified. In this case it would be:



              services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(MyLogging.Logger<>)));


              This allows all classes that require logging to use constructor injection as follows:



              class A
              {
              public A(Ilogger<A> logger) {...}
              }


              Implementation of ILogger which a wrapper to log4net should be rather simple. Please let me know if you need an example.



              Radek






              share|improve this answer


























                0












                0








                0







                The way to do it is to implement and then register your own strongly typed Logger class that derives from ILogger.



                .Net Core allows from to register generic type interface implementation with no type specified. In this case it would be:



                services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(MyLogging.Logger<>)));


                This allows all classes that require logging to use constructor injection as follows:



                class A
                {
                public A(Ilogger<A> logger) {...}
                }


                Implementation of ILogger which a wrapper to log4net should be rather simple. Please let me know if you need an example.



                Radek






                share|improve this answer













                The way to do it is to implement and then register your own strongly typed Logger class that derives from ILogger.



                .Net Core allows from to register generic type interface implementation with no type specified. In this case it would be:



                services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(MyLogging.Logger<>)));


                This allows all classes that require logging to use constructor injection as follows:



                class A
                {
                public A(Ilogger<A> logger) {...}
                }


                Implementation of ILogger which a wrapper to log4net should be rather simple. Please let me know if you need an example.



                Radek







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 16:59









                Radek StrugalskiRadek Strugalski

                1269




                1269






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431599%2fregistering-log4net-named-logger-net-core-service-provider%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'