我在 ASP.NET Core 2.1 中使用 Serilog,并通过appsettings.json.
默认模板不包含{SourceContext},所以我使用自己的包含它的模板。但我也希望使用 JSON 进行结构化日志记录。
我在 Serilog wiki 的某个地方阅读了我无法同时指定formatter(对于 JSON 输出)的内容outputTemplate。
所以我不能这样做,例如:
"outputTemplate": "{Timestamp:yyyy-MM-dd} {Level:u3} {SourceContext} {Message:lj}{NewLine}{Exception}",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
Run Code Online (Sandbox Code Playgroud)
那么如何获得JSON输出,同时获得SourceContext我需要的数据呢?
ASP.NET 核心 2.1
我认为这是自动设置的。我该如何防止?
Core2有一个钩子,用于验证从appsettings.json以下位置读取的选项:
services.PostConfigure<MyConfig>(options => {
// do some validation
// maybe throw exception if appsettings.json has invalid data
});
Run Code Online (Sandbox Code Playgroud)
此验证代码在首次使用时触发MyConfig,并在每次之后触发.所以我得到了多个运行时错误.
但是,在启动期间运行验证更为明智 - 如果配置验证失败,我希望应用程序立即失败.该文档暗示是如何工作的,但没有发生什么变化.
我做得对吗?如果是这样,这是设计,那么我怎样才能改变我正在做的事情,以便按照我想要的方式工作?
(另外,PostConfigure和之间的区别PostConfigureAll是什么?在这种情况下没有区别,所以什么时候应该使用哪一个?)
在 JavaScript 中:
function doSomething(mybar) {
if (!(mybar instanceof Bar)) throw new TypeError("mybar");
// ...other guard clauses
}
Run Code Online (Sandbox Code Playgroud)
在打字稿中:
function doSomething(mybar: Bar) {
// is guard clause ALWAYS redundant?
// ...other guard clauses
}
Run Code Online (Sandbox Code Playgroud)
我喜欢用保护子句验证输入。因此,在 JavaScript 中,我会测试它是否mybar是 的一个实例Bar,如果不是,那么我会抛出一个TypeError.
在 TypeScript 中mybar保证是正确的类型?(从而消除了对第一个保护条款的需要)
更新
下面有一些很好的答案,范围从“这绝对有可能”到“它可能发生”到“它不可能发生”。
因此,也许表达问题的一个好方法是 - 如果可能在运行时提供错误的类型,那么发生这种情况的机制是什么?例如,打字中的错误?
这不会编译:
public hello(user?: User): void {
// ...do some stuff
console.log(user.toString()); // error: "Object is possibly undefined"
}
Run Code Online (Sandbox Code Playgroud)
这可以通过类型保护来修复:
if (!user) throw Error();
Run Code Online (Sandbox Code Playgroud)
但我想把它移到一个单独的函数中,所以我试过了
private guard(arg: unknown): arg is object {
if (!arg) throw Error();
}
Run Code Online (Sandbox Code Playgroud)
我试过
private guard(arg: unknown): arg is object {
return arg !== undefined;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
如何在单独的函数中编写自定义类型保护,以断言“未定义”?
我的monorepo:
/app1
package.json
/app2
package.json
/shared
package.json
Run Code Online (Sandbox Code Playgroud)
该shared/package.json有"name": "@company/shared"。
应用程序项目的package.json文件具有依赖关系"@company/shared": "file:../shared"。
在引用共享代码时,我想要一个“短”样式,当东西移动时也不太容易中断:
import { foo } from "@company/shared"
Run Code Online (Sandbox Code Playgroud)
但这不起作用,所以我必须这样做::
import { foo } from "../../../../../shared/src/something"
Run Code Online (Sandbox Code Playgroud)
我摆弄了两者package.json,tsconfig.json但没有成功。
我该如何设置?
尽管自动 400 响应很有用,但我不想向客户端发送验证错误。
这是响应正文:
{
"errors": {
"username": [
"'username' must not be empty."
],
...more errors
},
"title": "One or more validation errors occurred",
"status": 400,
"traceId": "xxx:yyy"
}
Run Code Online (Sandbox Code Playgroud)
但我想要的是默认的,没有错误:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "Bad Request",
"status": 400,
"traceId": "xxx:yyy"
}
Run Code Online (Sandbox Code Playgroud)
我以为我需要做的一切都已确定
options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = true;
Run Code Online (Sandbox Code Playgroud)
...但这没有任何作用。
我不想禁用此功能,我只是想抑制验证错误。我怎么做?
顺便说一句,我正在使用一种解决方法,通过手动创建响应正文,但我宁愿避免这种情况:
services.Configure<ApiBehaviorOptions>(apiBehaviorOptions => {
apiBehaviorOptions.InvalidModelStateResponseFactory = actionContext => {
var pd = new ProblemDetails();
pd.Type = apiBehaviorOptions.ClientErrorMapping[400].Link;
pd.Title = apiBehaviorOptions.ClientErrorMapping[400].Title;
pd.Status = 400;
pd.Extensions.Add("traceId", actionContext.HttpContext.TraceIdentifier);
return new BadRequestObjectResult(pd);
};
});
Run Code Online (Sandbox Code Playgroud) 我招摇用户界面会显示“参数的内容类型”与各条目:"application/json-patch+json","text/json","application/json",和"application/*+json"。
我只想要"application/json"。
回购上有一个类似的未解决问题,它使用以下视觉效果(旧版ui,但思路相同):
有什么办法可以设置这个吗?
Swashbuckle.AspNetCore版本4.0.1
Swashbuckle.AspNetCore.Filters版本4.5.5
我想使用 ansible 在 ubuntu 服务器上安装 docker。
环境:
- 本地/控制器服务器:ansible 2.8.4
- 远程服务器:ubuntu 18.04,自带python 3.6.7
剧本:
##### provision brand new ubuntu 18.04 server
# ...
##### setup docker
- name: install packages required by docker
apt:
update_cache: yes
state: latest
name:
- apt-transport-https
- ca-certificates
- curl
- gpg-agent
- software-properties-common
- name: add docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: add docker apt repo
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu bionic stable
state: present
- name: install docker
apt: …Run Code Online (Sandbox Code Playgroud) 许多网站分为两部分:
www.example.com:供访客使用的公共MVC / RazorPages服务器渲染应用程序app.example.com:供客户和管理员使用的私有WebAPI应用程序(可通过 SPA 访问)这两个应用程序共享很多东西,例如代码、数据库、样式,因此最好将它们放在一个解决方案中,也可能分成多个项目。我希望可以通过某种方式调整标准配置(例如Startup.cs)来做到这一点。
该文档不涵盖这种情况。这个问题有多种解决方案,但它们适用于旧版本的框架。
ASP.NET Core 5 是如何做到这一点的?
asp.net-core-mvc asp.net-core asp.net-core-webapi razor-pages asp.net-core-5.0
asp.net-core ×5
typescript ×3
c# ×2
.net-core ×1
ansible ×1
cookies ×1
docker ×1
node.js ×1
npm ×1
package.json ×1
python ×1
razor-pages ×1
serilog ×1
swagger ×1
swagger-ui ×1
swashbuckle ×1
tsconfig ×1
ubuntu ×1