Home > Enterprise >  Type caste Serilog in c#
Type caste Serilog in c#

Time:05-18

 public class WorkletSelectionForm : Form
 {  
        private readonly ILogger _logger;

        public WorkletSelectionForm( ILogger<WorkletSelectionForm> logger)
        {
            _logger = logger;
        }

        private void btnClick_click(object sender, System.EventArgs e)
        {
            MainForm _frm = new MainForm((ILogger<MainForm>)_logger);
        }
 }

Getting TypeCaste Run-Time Error: Cann't convert ILogger< WorkletSelectionForm> to ILogger< MainForm>

CodePudding user response:

You probably need to inject the ILogger<MainForm> into the constructor as well, these types and instances are not interchangable.

Try this

public class WorkletSelectionForm : Form
     {  
            private readonly ILogger _logger;
            private readonly ILogger<MainForm> _mainFormLogger;
    
            public WorkletSelectionForm(
                ILogger<WorkletSelectionForm> logger,
                ILogger<MainForm> mainFormLogger)
            {
                _logger = logger;
                _mainFormLogger = mainFormLogger;
            }
    
            private void btnClick_click(object sender, System.EventArgs e)
            {
                MainForm _frm = new MainForm(_mainFormLogger);
            }
     }
  • Related