Azure

Add ILogger Reference at Startup in Azure Operate

Introduction

 

ILogger interface is part of .NET Framework that’s used to jot down to a log file. The log file data may be the next varieties:

  • Information
  • Debug
  • Error
  • Warning
  • Essential

 

All of the above-mentioned logging are the most typical ones. However it could differ from challenge to challenge. You can too create your personal customized log with your personal naming conference.

 

Startup

  • Most tasks use Dependency injection.
  • We have now finish variety of providers which might be getting injected

We’re resolving a variety of parameters and doing a little validations even earlier than the precise `Enterprise Logic` executes. However what if one thing goes incorrect there? How do I do know what’s incorrect with my startup?

 

Features Startup

 

Additionally it is potential to get Ilogger reference straight within the startup and log your actions. How are you going to do it?

 

In Azure Features, the startup class is inherited from FunctionsStartup and from which you get the `Configure` methodology the place you construct your IOC Container.

 

A easy Startup class will appear to be under piece of code

  1. public class Startup: FunctionsStartup {  
  2.     public override void Configure(IFunctionsHostBuilder builder) {  
  3.         var config = new ConfigurationBuilder().AddJsonFile(“native.settings.json”, non-obligatory: true, reloadOnChange: true).AddEnvironmentVariables().Construct();  
  4.         builder.Providers.AddLogging();  
  5.           
  6.     }  
  7. }  

Now to get logging working right here, you must make use of the `LoggerFactory ` to create an occasion of `ILogger`. This is how I acquired it working

  1. public class Startup: FunctionsStartup {  
  2.     personal ILoggerFactory _loggerFactory;  
  3.     public override void Configure(IFunctionsHostBuilder builder) {  
  4.         var config = new ConfigurationBuilder().AddJsonFile(“native.settings.json”, non-obligatory: true, reloadOnChange: true).AddEnvironmentVariables().Construct();  
  5.         builder.Providers.AddLogging();  
  6.         ConfigureServices(builder);  
  7.     }  
  8.     public void ConfigureServices(IFunctionsHostBuilder builder) {  
  9.         _loggerFactory = new LoggerFactory();  
  10.         var logger = _loggerFactory.CreateLogger(“Startup”);  
  11.         logger.LogInformation(“Bought Right here in Startup”);  
  12.           
  13.     }  
  14. }  

Conclusion

 

I hope this helps lots of people. I actually struggled to search out this and get it working.  

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close