0

I want to be able to use Ilogger to log different things in different methods for my Azure Function. As a standard the Ilogger is used in the methods parameters. But this wont work for me as I need to be able to call my methods without parameters.

Example:

        public async Task Run([TimerTrigger("0 0 10 * * *")] Ilogger logger)
        {
            await Method2()
        }

        public static async Task<String> Method2()
        {
          Method3()
          //Here I need to also log using Ilogger.
        }

        public static async Task<String> Method3()
        {
          //Here I need to also log using Ilogger.
        }

The solution would be in my head to also have the Ilogger logger used as a parameter for the Method2. But this will lead to a problem, as I then need to have it in my Run method.

So I do not know how I am able to log in different methods.

I have in total 5 methods that it would be needed to log in.

I have done some research on the Ilogger, but I could not find anything relating to my direct problem.

I thought making a constructor with the Ilogger in like so:

   public class Function1
    {

        private static ILogger<Function1> logger;

        public Function1(ILogger<Function1> logger)
        {
            Function1.logger = logger;
        }

        public async Task Run([TimerTrigger("0 0 10 * * *")])
        {
            await Method2()
            logger.Loginformation("Log stuff");
        }

       public static async Task<String> Method2()
       {
         //Here I need to also log using Ilogger.
         logger.Loginformation("Log stuff");
       }
   ...
   }

I have read this could work. I have done some debugging with this and changed a bit, but without luck.

If anyone have some ideas or solution on how to solve this, please do let me know :)

EDIT:

I have made it so it is no longer static, and changed it to the following:

    public class Function1
    {

        private ILogger<Function1> logger;

        public Function1(ILogger<Function1> logger)
        {
            this.logger = logger;
        }

        public async Task Run([TimerTrigger("0 0 10 * * *")])
        {
            await Method2()
            this.logger.Loginformation("Log stuff");
        }

       public async Task<String> Method2()
       {
         this.logger.Loginformation("Log stuff");
       }
   ...
   }

I have no exceptions, I tried to debug this, but unfortunately without luck to find the reason for the problem.

  • Is there any reason you use a static property for the logger ? – Scryper Apr 11 '23 at 09:08
  • Yes. In order to use it in the constructor it needs to be static. – Alexander Thomsen Apr 11 '23 at 09:09
  • 4
    @AlexanderThomsen no, it does not. For example make it non-static and `this.logger = logger;`. Or (maybe better) use different names for ctor parameters and properties/fields. – Guru Stron Apr 11 '23 at 09:10
  • 1
    @GuruStron You are right, I can make it like you suggested, but I will still not be able to log. – Alexander Thomsen Apr 11 '23 at 09:11
  • 2
    This is the typical kind of corner you paint yourself into when using `static`. Stop using `static`. Go with the `class` approach (which you will hopefully rename to something that makes sense as opposed to a class called `Function1`) and drop `static` everywhere. Also drop it from `private static ILogger logger;` Forget `static` exists. – Mike Nakis Apr 11 '23 at 09:11
  • As @GuruStron said, it does not need to be static. See my answer below. Using a non-static property, you are able to access your logger without having to add it has method parameter – Scryper Apr 11 '23 at 09:11
  • And is the looger configurated correctly? Will it write in a file or in the console? – Álvaro García Apr 11 '23 at 09:12
  • @ÁlvaroGarcía It should be able to log it in the console. And can you explain what you mean with configurating the logger. As I have not configurated anything. – Alexander Thomsen Apr 11 '23 at 09:26
  • Well, you could use different loggers, for example Serilog is very common. And this needs to configure level of log, for example debug, warning, errors... Which logger are you using? – Álvaro García Apr 11 '23 at 09:35
  • Please provide full [mre], desired and actual output. – Guru Stron Apr 11 '23 at 09:40

2 Answers2

2

Do not use a static property for the logger :

 public class Function1
    {

        private readonly ILogger<Function1> logger;

        public Function1(ILogger<Function1> logger)
        {
            this.logger = logger;
        }

        public async Task Run([TimerTrigger("0 0 10 * * *")])
        {
            await Method2()
            this.logger.Loginformation("Log stuff");
        }

       public async Task<String> Method2()
       {
         //Here I need to also log using Ilogger.
         this.logger.Loginformation("Log stuff");
       }
   ...
   }

Using a non-static property, you will be able to access your logger from everywhere in your class. So no need to add it as a method parameter.

Making your method non-static will allow you to use this to access your instance related properties.

Scryper
  • 178
  • 1
  • 14
  • Thank you for the notice for the problem using Static. But the problem still continues. :) – Alexander Thomsen Apr 11 '23 at 09:27
  • Describe your problem, do you have any exception thrown, is it your IDE that highlights a line of code ? Something else ? – Scryper Apr 11 '23 at 09:29
  • I have no execptions thrown, no, and I have tried to debug it, but without luck to track the reason for it not logging. – Alexander Thomsen Apr 11 '23 at 09:37
  • I feel like this is more a configuration issue. See https://stackoverflow.com/a/63407486/10458514 – Scryper Apr 11 '23 at 09:40
  • I have been making some more reading, and found a video that shows what I want to do. I also followed this video, https://www.youtube.com/watch?v=NN9Rmf0PUG4, in hope that it would solve my issue. But it did not, I have still not logged anything yet. I have made my configurations to be what I believe is the correct way. – Alexander Thomsen Apr 11 '23 at 12:15
0

You definitely should not use a static property for the logger!

  • You can pass the logger instance as parameter to the static methods.

  • Or you make the static methods non-static and access the logger via a non static property.

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89