C#构造函数在执行代码后调用构造函数

时间:2017-06-20 10:24:01

标签: c# 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();
    }

此构造函数的一个特定客户端将不具有参数所需的值,因此我希望能够调用这个简单的构造函数,该构造函数将获得所需的值,然后调用第一个构造函数:

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

        SerilogHelper(conString, minLevel);
    }

问题是,我在第二个构造函数的调用中得到了一个红色波浪形,消息 SerilogHelper是一个'类型'但是像'变量'一样使用

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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

答案 2 :(得分: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 无效可以用作默认值,如果不是这样的话( any < / em> 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();
}