NestJS 使用默认记录器实现。它将输出发送到控制台。
我可以知道,如何配置默认记录器以将输出发送到文件、数据库。
此外,
如果我想在 NestJS 中使用 Winston,如何使用/注入/扩展各种传输选项。
它不应该与 NestJS 紧密耦合,并且始终能够替换为其他一些记录器。
我已经在 Windows 10 上安装了 VS 2019。创建了 ASP.net Core Web 项目 -> 选定的 API。当我尝试生成引用模型的控制器并创建上下文类时,它没有生成控制器类,但它给了我以下错误:
错误,运行所选代码生成器时出错
'未处理的异常。System.IO.FileNotFoundException:无法加载文件或程序集“Microsoft.VisualStudio.Web.CodeGeneration.Utils,版本=3.1.2.0,Culture=neutral,PublicKeyToken=adb9793829ddae60”。该系统找不到指定的文件。文件名:Microsoft.VisualStudio.Web.CodeGeneration.Design.Program.Main(String[] args) 中的“Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”
我想为具有 Catch 方法的 Nest JS Filter 编写单元测试。如何传递/模拟发生异常时传递的参数。如何断言 logger.error 被调用。
Jest用于单元测试
这是捕获所有异常的 Nest JS Filter 代码。
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
constructor(private logger: AppLoggerService) {}
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const message = {
Title: exception.name,
Type: 'Error',
Detail: exception.message,
Status: 'Status',
Extension: '',
};
this.logger.error(message, '');
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
response.status(status).send({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
} …Run Code Online (Sandbox Code Playgroud) 我使用 Nest.js 并实现 LoggerService 来自定义记录器。在实现中,它使用 Winston 记录器并且工作正常。
import { LoggerService } from '@nestjs/common';
import * as winston from 'winston';
import { config } from '../config';
import * as path from 'path';
export class AppLogger implements LoggerService {
private logger: winston.Logger;
constructor(label?: string) {
this.initializeLogger(label);
}
initializeLogger(label?: string) {
this.logger = winston.createLogger({
level: config.logger.level,
format: winston.format.json(),
transports: [
new winston.transports.File({ dirname: path.join(__dirname, './../log/debug/'), filename: 'debug.log', level: 'debug' }),
new winston.transports.File({ dirname: path.join(__dirname, './../log/error/'), filename: 'error.log', level: 'error' }),
new winston.transports.File({ dirname: …Run Code Online (Sandbox Code Playgroud) 使用 JSON Schema 验证 JSON 始终返回 true。Newtonsoft 用于验证并在此处使用模式和数据进行测试。它总是返回“未发现错误。JSON 根据架构进行验证。
请找到我的 JSON 架构。
{
"schema": {
"definitions": {
},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"widget": { "formlyConfig": { "type": "accordion" } },
"title": "The Root Schema",
"required": [
"accordion1",
"accordion2",
"accordion3"
],
"properties": {
"accordion1": {
"$id": "#/properties/accordion1",
"type": "object",
"title": "The Accordion1 Schema",
"required": [
"firstname",
"age"
],
"properties": {
"firstname": {
"$id": "#/properties/accordion1/properties/firstname",
"type": "string",
"title": "The Firstname Schema",
"default": "firstname pvr1"
},
"age": {
"$id": …Run Code Online (Sandbox Code Playgroud) 这是我用 Typescript 为 Nodejs/Nestjs 编写的 BadRequestExceptionFilter
@Catch(BadRequestException)
export class BadRequestExceptionFilter implements ExceptionFilter {
constructor(private logger: AppLoggerService) {}
catch(exception: BadRequestException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const status =
exception instanceof BadRequestException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const message = {
Title: exception.message.error,
Type: 'Exception - BadRequestExceptionFilter',
Detail: exception.message,
Status: '',
};
this.logger.error(message, '');
response.code(status).send({
statusCode: status,
...(exception.getResponse() as object),
timestamp: 'Exception - BadRequestException' + new Date().toISOString(),
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试和 2 个断言在这里完成。第一个断言是检查是否调用了 …
node.js ×4
c# ×2
jestjs ×2
logging ×2
nestjs ×2
typescript ×2
unit-testing ×2
winston ×2
asp.net-core ×1
json ×1
json.net ×1
jsonschema ×1