我在 Angular 8 和 Asp.net WebApi 2 中工作。Angular 我在 ErrorHandlerService 中调用记录器
import { ErrorHandler, Injectable, Injector } from '@angular/core';
import { AppConfigService } from '@core/app-config/app.config.service';
import { NGXLogger } from 'ngx-logger';
@Injectable()
export class ErrorHandlerService implements ErrorHandler {
constructor(private injector: Injector, private logger: NGXLogger) {
}
handleError(error: any): void {
this.logger.error(error);
}
}
// Registering in App module -->
LoggerModule.forRoot({serverLoggingUrl: '/api/loggs/client', level: NgxLoggerLevel.DEBUG, serverLogLevel: NgxLoggerLevel.DEBUG}),
],
providers: [
{ provide: ErrorHandler, useClass: ErrorHandlerService }
],
\\ Server side coding :
public class LogPayload
{
public LogLevelEnum Level { get; set; }
public double Timestamp { get; set; }
public string FileName { get; set; }
public string LineNumber { get; set; }
public string Message { get; set; }
}
[Route("api/loggs/client")]
[HttpPost]
public void LogClient(LogPayload log)
{
_Service.RegisterLog(log);
}
public void RegisterLog(LogPayload log)
{
// read log details
log file deatils:
log.FileName :main.js
log.Level = Error
log.LineNumber = 18494
log.Message ="Http failure response"
}
不确定如何从 Angular 组件获取组件名称而不是带有行号的 main.js?而且 Timestamp 总是返回 0 。我们是否必须使用 export class NGXLogInterface { level: NgxLoggerLevel; 时间戳:字符串;文件名:字符串;行号:字符串;消息:字符串;附加:任何[];在 Angular 中传递 post 请求中的每个项目?服务器日志记录没有太多文档。任何帮助都会很棒。