Set property values for a registered dependency

MattNorman

Well-known member
Joined
May 22, 2021
Messages
98
Programming Experience
1-3
I have a logger class that is registered in the program.cs class of any asp.net core app.

After the user logs in, I would like to set some properties of that class (username etc) so that I don;t have to pass it to the logger every time I need to log an error.

Is it possible to set property values for a registered dependency and those properties then be available whenever it is injected?
 
Which IOC are you using?
 
I have a logger class that is registered in the program.cs class of any asp.net core app.

After the user logs in, I would like to set some properties of that class (username etc) so that I don;t have to pass it to the logger every time I need to log an error.

Is it possible to set property values for a registered dependency and those properties then be available whenever it is injected?
I am using the native IOC with .NET 6 as follows:

C#:
var builder = WebApplication.CreateBuilder(args);

// Add busines logic services.
builder.Services.AddScoped<BLAccount>();
builder.Services.AddScoped<BLAccountType>();
builder.Services.AddScoped<BLAuditLog>();
builder.Services.AddScoped<BLErrorLog>();

At this stage the user has not been authenticated. I only have the user details during the login post action.

Previously I have had each layer of my application have a class that retrieves the data from the session but this seems a bit clunky.
 
Back
Top Bottom