C # constructor to call the constructor after code execution

I saw the answers to the chain of constructors, but they do not apply to my problem.

I have the following constructor that requires a couple of parameters:

public SerilogHelper(string conString, int minLevel) { var levelSwitch = new LoggingLevelSwitch(); levelSwitch.MinimumLevel = (Serilog.Events.LogEventLevel)(Convert.ToInt32(minLevel)); _logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .WriteTo.MSSqlServer(connectionString: conString, tableName: "Logs", autoCreateSqlTable: true) .CreateLogger(); } 

One particular client of this constructor will not have the values โ€‹โ€‹needed for the parameters, so I would like to be able to call this simple constructor, which will get the required values, and then call the 1st constructor:

 public SerilogHelper() { string minLevel = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.level"); string conString = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.connectionstring"); SerilogHelper(conString, minLevel); } 

The problem is that I get red squiggly when called to the second constructor with the message SerilogHelper is a "type" but is used as a "variable"

+5
source share
3 answers

Why not just add these options?

 // this assumes that SSOSettingsFileManager is static class // this will automatically call these methods before passing // values to another ( non parameterless ) constructor public SerilogHelper() : this ( SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.connectionstring" ), SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.level" ) ) { } // This will be called from default ( parameterless ) // constructor with values retrieved from methods // called in previous constructor. public SerilogHelper(string conString, int minLevel) { var levelSwitch = new LoggingLevelSwitch(); levelSwitch.MinimumLevel = (Serilog.Events.LogEventLevel)(Convert.ToInt32(minLevel)); _logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .WriteTo.MSSqlServer(connectionString: conString, tableName: "Logs", autoCreateSqlTable: true) .CreateLogger(); } 

Test online

+7
source

You cannot do this. The best option you have is to move the initialization code to a separate method that you can call from both constructors. It is allowed.

 public SerilogHelper() { string minLevel = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.level"); string conString = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.connectionstring"); this.Initialize(conString, minLevel); } public SerilogHelper(string conString, int minLevel) { this.Initialize(conString, minLevel); } protected void Initialize(string conString, int minLevel) { var levelSwitch = new LoggingLevelSwitch(); levelSwitch.MinimumLevel = (Serilog.Events.LogEventLevel)(Convert.ToInt32(minLevel)); _logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .WriteTo.MSSqlServer(connectionString: conString, tableName: "Logs", autoCreateSqlTable: true) .CreateLogger(); } 
+7
source

I suggest using the default values:

 public SerilogHelper(string conString = null, int minLevel = -1) { if (minLevel == -1) minLevel = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.level"); if (conString == null) conString = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.connectionstring"); var levelSwitch = new LoggingLevelSwitch(); levelSwitch.MinimumLevel = (Serilog.Events.LogEventLevel)(Convert.ToInt32(minLevel)); _logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .WriteTo.MSSqlServer(connectionString: conString, tableName: "Logs", autoCreateSqlTable: true) .CreateLogger(); } 

....

 SerilogHelper myInstance = new SerilogHelper(); 

Edit: I assumed that minLevel = -1 is invalid, can be used as the default if it is not (any minLevel values โ€‹โ€‹are minLevel ) int? output:

 public SerilogHelper(string conString = null, int? minLevel = null) { if (!minLevel.HasValue) minLevel = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.level"); if (conString == null) conString = SSOSettingsFileManager.SSOSettingsFileReader.ReadString( "LCC.Common", "serilog.connectionstring"); var levelSwitch = new LoggingLevelSwitch(); levelSwitch.MinimumLevel = (Serilog.Events.LogEventLevel)(Convert.ToInt32(minLevel)); _logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .WriteTo.MSSqlServer(connectionString: conString, tableName: "Logs", autoCreateSqlTable: true) .CreateLogger(); } 
+2
source

Source: https://habr.com/ru/post/1269024/


All Articles