2017-06-20 61 views
5

我在構造函數鏈中看到了答案,但它們不適用於我的問題。執行代碼後調用構造函數的C#構造函數

我有下面的構造函數,需要幾個參數:

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(); 
    } 

此構造不會對參數所需的值的一個特定的客戶端,所以我希望能夠調用這個簡單的構造函數,會得到所需的值,然後撥打第一個構造函數:

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

     SerilogHelper(conString, minLevel); 
    } 

問題是,我得到一個紅色波浪上的呼叫與消息SerilogHelper 2號構造是一個「型」,但一樣使用一個 '變量'

+0

[C#構造超載]的可能的複製(https://stackoverflow.com/questions/5555715/c-sharp-constructors-overloading) – Liam

回答

7

爲什麼不只是簡單地添加這些參數?

// 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

你不能那樣做。最好的選擇是將初始化代碼移動到可以從兩個構造函數中調用的單獨方法。那是允許的

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(); 
} 
2

我建議使用默認值

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(); 

編輯:我認爲minLevel = -1無效可以使用作爲默認值,如果不是這種情況(任何minLevel值是允許的)int?是出一個可行的辦法:

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

即只能用於當'conString = = null'和'minLevel == -1'不是有效的值。 –

+1

@Patrick Hofman:很對;如果*任意*'minLevel'被允許,那麼'int? minLevel = null'是一個可能的出路。 –

+0

好點。那是一個出路。 –