Base solution for your next web application
Open Closed

Logger does not work if used outside App Service classes #605


User avatar
0
khaled created

Hi,

why Logger does not log when use it with classes other than app service classes, can any one tell me where is the error here:

//SomeClass.cs
public class SomeClass : ITransientDependency
    {
        public ILogger Logger { get; set; }

        public SomeClass ()
        {
            Logger = NullLogger.Instance;
        }

        internal async Task SomeMethod()
        {
            //.................
            //log message does not work
            Logger.Info("message");
            //..............
        }
    }

//DemoApplicationModule.cs
[DependsOn(typeof(DemoCoreModule))]
    public class DemoApplicationModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            IocManager.Resolve<SomeClass>();       
        }
    }

8 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    It works everywhere, not only app services.

    SomeClass is correct but where did you call SomeMethod() ? I did not see it.

  • User Avatar
    0
    khaled created

    Hi Halil, thanks for reply. I call somemethod from my app services:

    public class someAppService : ApplicationService, ISomeAppService
        {
                   private readonly SomeController _someController;
                   public Task SomeAction()
                   {
                            //...........
                            _someController.SomeMethod();
                           // ............
                   }
    
  • User Avatar
    0
    khaled created

    All code at somemethod() executed normally except logging and if I call logger inside someAppService it works and i can see the loggs.

    one another question, How can I implement custom action filters on someAppService like apbAuthorize ?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can create an attribute and an Castle Windsor interceptor which checks and executes it. ABP highly uses interceptors.

  • User Avatar
    0
    khaled created

    Ok, How about Logging :), Am I missing something?

  • User Avatar
    0
    hikalkan created
    Support Team

    Can you check if Logger is NullLogger? Maybe somehow DI not working in your case. But sure that Logger is not a special thing and works in every class.

  • User Avatar
    0
    khaled created

    Yes, It is NullLogger, but WHY :o ? I tried calling the Logger directly after resolving in DemoApplicationModule.cs and it works:

    //DemoApplicationModule.Initialize()
    //...
    var someObj = IocManager.Resolve<SomeClass>(); 
    someObj.Logger.Info("Testing...");//it works here
    someObj.SomeMethod();//this logs also...
    //...
    
  • User Avatar
    0
    khaled created

    Actually, I want to tell you that it works in every class :D. Many thanks Halil.