c# - Passing logger to controller with carter - Stack Overflow

I'm playing with Carter and so far it works fine, but I have an issue when I want to pass logger (

I'm playing with Carter and so far it works fine, but I have an issue when I want to pass logger (for now its NullLogger, later on I will replace it with something more menaingful).

System.InvalidOperationException: An action cannot use both form and JSON body parameters.
Below is the list of parameters that we found: 

Parameter           | Source                        
---------------------------------------------------------------------------------
file                | Form File (Inferred)
logger              | Body (Inferred)

How do I pass ILogger from Microsoft.Extensions.Logging.Abstractions and other things that I would have in builder.Services into Carter endpoints?

Here is my code:

using Carter;
using Microsoft.Extensions.Logging.Abstractions;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at 
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddLogging(l => l.AddProvider(NullLoggerProvider.Instance));
builder.Services.AddCarter();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapCarter();

app.UseHttpsRedirection();

app.Run();

And here is the controller:

using Carter;

namespace RESTDemo.Controllers
{
    public class ParseController : ICarterModule
    {
        public void AddRoutes(IEndpointRouteBuilder app)
        {
            var group = app.MapGroup("api/parse");

            group.MapPost("", ParseFile).DisableAntifery();
        }

        private IResult ParseFile(IFormFile file, ILogger logger)
        {
            logger.LogInformation("ParserExecuted");

            return Results.Ok();
        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744796093a4594210.html

相关推荐

  • c# - Passing logger to controller with carter - Stack Overflow

    I'm playing with Carter and so far it works fine, but I have an issue when I want to pass logger (

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信