DotNETCore 学习笔记 日志
Logging
---------------------------------------------------------------------------------------
Implementing Logging in your Application var logger = loggerFactory.CreateLogger("Catchall Endpoint");
logger.LogInformation("No endpoint found for request {path}", context.Request.Path); [Route("api/[controller]")]
public class TodoController : Controller
{
private readonly ITodoRepository _todoRepository;
private readonly ILogger<TodoController> _logger; public TodoController(ITodoRepository todoRepository,
ILogger<TodoController> logger)
{
_todoRepository = todoRepository;
_logger = logger;
} [HttpGet]
public IEnumerable<TodoItem> GetAll()
{
_logger.LogInformation(LoggingEvents.LIST_ITEMS, "Listing all items");
EnsureItems();
return _todoRepository.GetAll();
} Logging Verbosity Levels Trace
Used for the most detailed log messages, typically only valuable to a developer debugging an issue. These messages may contain sensitive application data and so should not be enabled in a production environment. Disabled by default. Example: Credentials: {"User":"someuser", "Password":"P@ssword"} Debug
These messages have short-term usefulness during development. They contain information that may be useful for debugging, but have no long-term value. This is the default most verbose level of logging. Example: Entering method Configure with flag set to true Information
These messages are used to track the general flow of the application. These logs should have some long term value, as opposed to Verbose level messages, which do not. Example: Request received for path /foo Warning
The Warning level should be used for abnormal or unexpected events in the application flow. These may include errors or other conditions that do not cause the application to stop, but which may need to be investigated in the future. Handled exceptions are a common place to use the Warning log level. Examples: Login failed for IP 127.0.0.1 or FileNotFoundException for file foo.txt Error
An error should be logged when the current flow of the application must stop due to some failure, such as an exception that cannot be handled or recovered from. These messages should indicate a failure in the current activity or operation (such as the current HTTP request), not an application-wide failure. Example: Cannot insert record due to duplicate key violation Critical
A critical log level should be reserved for unrecoverable application or system crashes, or catastrophic failure that requires immediate attention. Examples: data loss scenarios, out of disk space _logger.LogInformation(LoggingEvents.LIST_ITEMS, "Listing all items");
_logger.LogInformation(LoggingEvents.GET_ITEM, "Getting item {0}", id); Log Level Prefix
Critical crit
Error fail
Warning warn
Information info
Debug dbug
Trace trce loggerFactory
.WithFilter(new FilterLoggerSettings
{
{ "Microsoft", LogLevel.Warning },
{ "System", LogLevel.Warning },
{ "ToDoApi", LogLevel.Debug }
})
.AddConsole(); "Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Filter": "1.0.0",
"Microsoft.Extensions.Logging.TraceSource": "1.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": { // add Trace Source logging
var testSwitch = new SourceSwitch("sourceSwitch", "Logging Sample");
testSwitch.Level = SourceLevels.Warning;
loggerFactory.AddTraceSource(testSwitch,
new TextWriterTraceListener(writer: Console.Out)); [HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(string id)
{
_logger.LogInformation(LoggingEvents.GET_ITEM, "Getting item {0}", id);
var item = _todoRepository.Find(id);
if (item == null)
{
_logger.LogWarning(LoggingEvents.GET_ITEM_NOTFOUND, "GetById({0}) NOT FOUND", id);
return NotFound();
}
return new ObjectResult(item);
}
DotNETCore 学习笔记 日志的更多相关文章
- Linux多线程服务端编程 使用muduo C++网络库 学习笔记 日志log
代码来自陈硕开源代码库 muduo中 地址是https://github.com/chenshuo/muduo #pragma once #include <string> #define ...
- UI自动化学习笔记- 日志相关操作
一.日志相关 1.日志 概念:日志就是用于记录系统运行时的信息,对一个事件的记录,也称log 1.1 日志的作用 调试程序 了解系统程序运行的情况,是否正常 系统程序运行故障分析与问题定位 用来做用户 ...
- Mybatis学习笔记-日志
日志工厂 如果一个数据库操作出现异常,在排错时,则需要日志 SLF4J Apache Commons Logging(COMMONS_LOGGING) LOG4J LOG4J2 JDK logging ...
- DotNETCore 学习笔记 WebApi
API Description Request body Response body GET /api/todo Get all to-do items None Array of to-do ite ...
- DotNETCore 学习笔记 MVC视图
Razor Syntax Reference Implicit Razor expressions <p>@DateTime.Now</p> <p>@DateTim ...
- DotNETCore 学习笔记 宿主
Hosting -------------------------------------------------------------------------- Setting up a Host ...
- DotNETCore 学习笔记 依赖注入和多环境
Dependency Injection ------------------------------------------------------------------------ ASP.NE ...
- DotNETCore 学习笔记 配置
Configuration var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(); var config ...
- DotNETCore 学习笔记 全球化和本地化
Globalization and localization ********************************************************************* ...
随机推荐
- jmeter插件之jsonpath提取响应结果和做断言
准备工作: 1. jmeter3.X已经自带了提取响应结果的插件:JSON Extractor 2. 下载断言插件:https://jmeter-plugins.org/wiki/JSONPathAs ...
- Qt Qwdget 汽车仪表知识点拆解7 图像绘制,旋转
先贴上效果图,注意,没有写逻辑,都是乱动的 看下最中心的指针旋转,这里使用的QPainter的绘制函数 要显示复杂的效果,需要分层 void Widget::draw_number_pointer() ...
- Mybatis 异常记录(1): Invalid bound statement (not found)
错误信息: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pingan.cr ...
- C语言关于“输入包含多行数据,请处理到文件结束”的问题
今天,笔者在做本校ACM校赛网络赛的时候,遇到输入格式中有这样的要求:输入包含多行数据,请处理到文件结束.题目的逻辑很简单,主要功能代码很容易实现,但是题目中没有“明确”指出控制台中输入数据以什么方式 ...
- 点击查看大图Activity
1.使用方式 Intent intent = new Intent(FriendCircleActivity.this, ImageGralleryPagerActivity.class);//0,索 ...
- 修改maven远程仓库为阿里的maven仓库(复制)
maven之一:maven安装和eclipse集成 maven作为一个项目构建工具,在开发的过程中很受欢迎,可以帮助管理项目中的bao依赖问题,另外它的很多功能都极大的减少了开发的难度,下面来介绍ma ...
- Android Service 服务(二)—— BroadcastReceiver
(转自:http://blog.csdn.net/ithomer/article/details/7365147) 一. BroadcastReceiver简介 BroadcastReceiver,用 ...
- poi解析excel出现格式不正确
后缀为xlsx的excel做系统导入时出现bug: Strict OOXML isn't currently supported, please see bug #57699 为了同时兼容03.07及 ...
- hibernate多表查询
一对多进行查询(用懒加载的模式) 查找区域所对应的街道: Dao: public Qu selQu(String dno){ Session session=HibernateSessionFacto ...
- BZOJ 题解continue
1041 圆上的整点 暴力枚举 会超时 这道题很像之前一次noip模拟题(当时的我还太水了(虽然现在也很水)) x2+y2=R2 考虑变型 x2=(R+y)(R-y) int d=gcd(R,y) i ...