0

I read some posts here on SO regarding this issue but they don't help. I got an error of ambiguous refrence. According to this article: Use Serilog with Microsoft.Extensions.Logging.ILogger I should add .UseSerilog() to builder. I did so but still have a reference clash. I use .Net Core 5.0

I installed Serilog and Miscrosoft extensions packages:

    <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
    <PackageReference Include="Serilog" Version="2.12.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />

Program.cs

        public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(config)
                .CreateLogger();

            var host = CreateHostBuilder(args).Build();         

            host.Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .UseSerilog();
    }

I also added dependency:

services.AddSingleton(Log.Logger);

And in controller I got ILogger<CommissionController> underlined with this ambiguous error:

    public class CommissionController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger _logger;

        public CommissionController(IMediator mediator, ILogger<CommissionController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        ....
mustafa00
  • 751
  • 1
  • 7
  • 28
  • Your controller doesn't need to know about serilog. Somehow it got in there either by global or explicit using. – Ralf Apr 05 '23 at 09:37
  • Ok, so if I undersand correctly - I don't need to use DI, I can just use global using? – mustafa00 Apr 05 '23 at 09:43
  • 1
    No you misunderstood. You SHOULD use DI. And any user like your controller then only needs to now about the interfaces registered but not how it with implemented by a concrete class. And if its sees Serilog you did something wrong all it should see is the ILogger interface from the framework ignoring with what class it was backed. Maybe add the usings to the question you used in the file for CommissionController – Ralf Apr 05 '23 at 09:46

1 Answers1

0

You most likely need to disable implicit usings in your *.csproj:

    <PropertyGroup>
        <ImplicitUsings>disable</ImplicitUsings>
    </PropertyGroup>
t3chb0t
  • 16,340
  • 13
  • 78
  • 118