小编itm*_*nnz的帖子

带有类名、方法名的 Serilog 日志记录

我在我的项目 Angular、web api、EF 中使用了 serilog 和 SEQ。我对两者都是新手。

1) 如何确保每次我写错误、信息、调试时都应该包含 ClassName.Method Name。我知道我必须创建 Enrich 但不确定如何获取 ClassName.Method Name

例如

Class Test
{
  public void testing(){
  // logger.Error("Error ....");}
}
Run Code Online (Sandbox Code Playgroud)

现在,当我看到日志时,它应该显示“29/09/2012 10:00:00, Test=>testing Error .....”

在,短 DateTime、ClassName、MethodName 和 Message

logging serilog

6
推荐指数
1
解决办法
4896
查看次数

swagger 不显示 HttpGet 查询参数

我正在使用 swagger 开发 asp.net core webapi(6.0)。当我运行应用程序 HttpGet 方法时,参数摘要在 swagger 中不可见。例如 Id、StoreId、DateFrom、DateTo 摘要不会在 swagger 中显示。请指教

 /// <summary>
    /// Get product details.
    /// </summary>
    /// <remarks>Fetches product.</remarks>
    /// <param name="parameters"></param>
    /// <returns></returns>
    /// <response code="200">Successful Response</response>
    /// <response code="400">Bad Request</response>
    /// <response code="401">Unauthorized</response>
    /// <response code="424">Failed Dependency</response>
    /// <response code="500">Internal Server Error</response>
    public async Task<IActionResult> GetProduct([FromQuery] Parameter parameters)
        {
            var repo = await Repository.GetAllAsync(parameters);
            return Ok()
        }



public class Parameter
{
    /// <summary>
    /// Product Id
    /// </summary>
    public string? …
Run Code Online (Sandbox Code Playgroud)

asp.net-core asp.net-core-webapi

6
推荐指数
1
解决办法
4310
查看次数

在搭建现有数据库 EF core 2.X 后更新模型

我正在使用 EF core 2.X 脚手架现有数据库。我使用“dotnet ef dbcontext scaffold”命令生成了模型类,它生成了模型类。

数据库团队更改了一些表,我必须再次运行“dotnet ef dbcontext scaffold”命令以生成模型类以仅选择更改。

例如,假设我有一张名为“employee”的表,其中包含列 id、name。

我运行“dotnet ef dbcontext scaffold”来生成模型

之后我更改了员工表并在数据库中添加了一个名为“地址”的列。我如何构建命令以仅选择更改。

注意:我知道在生成模型后我应该使用迁移来更改数据库,但我们的数据库团队已经更改了数据库,不幸的是,我必须这样做。和建议

entity-framework-core

5
推荐指数
1
解决办法
4163
查看次数

Serilog ForContext 未按预期工作

我在我的项目(MVC/web api 等)中使用 serilog & SEQ 和 Autofac (DI)。虽然它工作正常,但不确定这是正确的方法。

我有几个问题。请帮忙

Q1) 如何使 LoggerConfiguration 通过 Web.config (appsetting) 进行管理,例如 Verbose/Debug 等。

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()                    
    .WriteTo.Seq(serilogUrl)
    .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

Q2)对于Everymessage,我想写userid。我已经使用了推送属性,但没有“使用”语句。见下面的代码

public partial class Repo : BaseRepo<db>
    {
        public Repo(ILogger logger) : base(logger)
        {
            var currentuser = GetUserName();
            LogContext.PushProperty("User Name", currentuser);        
            Logger.ForContext<Repo>();
        }
  public void somefunction()
  { 
    try{}
    catch(exception e){
          Logger.Error(e, "Message");
        }
  }
}
Run Code Online (Sandbox Code Playgroud)

Q3)在构造函数中,我使用了 Logger.ForContext(),假设这会将类名写入每条消息。但它不起作用。

   Logger.ForContext<Repo>()
Run Code Online (Sandbox Code Playgroud)

注意:我没有使用asp.net core/.Net core

serilog

4
推荐指数
1
解决办法
4872
查看次数

流畅验证自定义响应 ASP.NET Core Web API

我正在开发一个 ASP.NET Core 6 Web API 项目。我们使用流畅的验证。如何返回自定义响应。请指教。

我想发送关于不同验证错误的自定义响应,例如页面、限制、日期等

这是我默认得到的响应:

  {
       "type": "",
       "title": "One or more validation errors occurred.",
       "status": 400,
       "traceId": "00-097c689850af328c49705cea77fc6fbd-0c7defe165d9693f-00",
       "errors": {
         "Page": [
           "Page value should be greater than 0"
         ],
         "Limit": [
           "Limit value should be greater than 0"
         ]
       }
}
Run Code Online (Sandbox Code Playgroud)

这就是我想得到的回应:

{
  "traceId": "e45b3398-a2a5-43eb-8851-e45de1184021",
  "timestamp": "2021-06-28T00:32:06.548Z",
  "errors": [
    {
      "errorCode": "Contract Violation",
      "message": "Page should be greater than 0",
      "priority": "MEDIUM",
      "properties": {
        "name": "string",
        "value": "string"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

public …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-webapi

4
推荐指数
1
解决办法
4213
查看次数