Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 441 forks source link

Proper way to access ILogger and IConfiguration inside Startup #4577

Open APIWT opened 5 years ago

APIWT commented 5 years ago

Is your question related to a specific version? If so, please specify:

v2 (latest)

What language does your question apply to? (e.g. C#, JavaScript, Java, All)

C#

Question

What is the proper way to get access to IConfiguration and ILogger inside a custom startup class?

erionpc commented 2 years ago

@PureKrome thanks. That works. I wasn't aware. That saves me from having to add another config file.

trinzya commented 1 month ago

@PureKrome I am sorry, I have still confused, I know this is still open what is the best way of configuring serilog in function app, Should weuse appsetting.json should for Serilog or like you mentioned, have key values pair in azure app setting and local.settings.json and use that. What is the preferred way and why? It would so helpful to have the clear understanding on that. I am super new to azure function app.

PureKrome commented 1 month ago

@trinzya you have two options here

1. Azure's dev's built-in pattern/design 🌄

this is: "use local.settings.json. There are opinionated design-reasons*** for this. so just stick with this.

2. Customize your own way 🏗️

this is: use appsettings.<env>.json ... but guess what? You're going to have to customize the start up code to tell the azure functions to read in these files. blah blah blah.

Preferred way? 🔄️

Azure devs will say the preferred way is their way (option 1 above) Some other people might say: stick with custom way (option 2 above) because that brings in some consistency with our own team of people who work with ASP.NET Core etc etc blah blah blah.

Me? 👨🏻‍💻

I've picked the option-1 because i'm old and don't want to fight the system. I've had my cry, rolled around on the floor, picked myself up and moved on. I've stopped trying to fight over small things. I've made peace 🕊️ with my inner OCD.

trinzya commented 1 month ago

@PureKrome Thank you! In my case, my company prefers the second option. Our simple and fun explanation makes it crystal clear for me why 🙂