如何高效选择合适的日志级别和存储方式-asp.net写日志时

教程大全 2026-01-22 05:56:28 浏览

在ASP.NET中编写日志是确保应用程序性能和调试过程顺利进行的重要部分,良好的日志记录实践可以帮助开发者和系统管理员追踪错误、监控应用程序行为以及优化性能,以下是如何在ASP.NET中编写日志的详细指南。


python里面test.log 是什么意思

高效ASP.NET日志存储方案

1. logging介绍Python的logging模块提供了通用的日志系统,可以方便第三方模块或者是应用使用。 这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTTP GET/POST,smtp,Socket等,甚至可以自己实现具体的日志记录方式。 logging模块与log4j的机制是一样的,只是具体的实现细节不同。 模块提供logger,handler,filter,formatter。 logger:提供日志接口,供应用代码使用。 logger最长用的操作有两类:配置和发送日志消息。 可以通过(name)获取logger对象,如果不指定name则返回root对象,多次使用相同的name调用getLogger方法返回同一个logger对象。 handler:将日志记录(log record)发送到合适的目的地(destination),比如文件,socket等。 一个logger对象可以通过addHandler方法添加0到多个handler,每个handler又可以定义不同日志级别,以实现日志分级过滤显示。 filter:提供一种优雅的方式决定一个日志记录是否发送到handler。 formatter:指定日志记录输出的具体格式。 formatter的构造方法需要两个参数:消息的格式字符串和日期字符串,这两个参数都是可选的。 与log4j类似,logger,handler和日志消息的调用可以有具体的日志级别(Level),只有在日志消息的级别大于logger和handler的级别。 [python] view plain Copy print?import loggingimport _FILE = = (LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler fmt = %(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)sformatter = (fmt) # 实例化(formatter)# 为handler添加formatterlogger = (tst)# 获取名为tst的(handler) # 为logger添加()(first info message)(first deBug message)输出:[plain] view plain copy print?2012-03-04 23:21:59,682 - log_:16 - tst - first info message 2012-03-04 23:21:59,682 - log_:17 - tst - first debug message关于formatter的配置,采用的是%()s的形式,就是字典的关键字替换。 提供的关键字包括:FormatDescription%(name)sName of the logger (logging channel).%(levelno)sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).%(levelname)sText logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).%(pathname)sFull pathname of the source file where the logging call was issued (if available).%(filename)sFilename portion of pathname.%(module)sModule (name portion of filename).%(funcName)sName of function containing the logging call.%(lineno)dSource line number where the logging call was issued (if available).%(created)fTime when the LogRecord was created (as returned by ()).%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.%(asctime)sHuman-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time).%(msecs)dMillisecond portion of the time when the LogRecord was created.%(thread)dThread ID (if available).%(threadName)sThread name (if available).%(process)dProcess ID (if available).%(message)sThe logged message, computed as msg % args.这个是摘自官网,提供了很多信息。 2. logging的配置logging的配置可以采用python代码或是配置文件。 python代码的方式就是在应用的主模块中,构建handler,handler,formatter等对象。 而配置文件的方式是将这些对象的依赖关系分离出来放在文件中。 比如前面的例子就类似于python代码的配置方式。 这里看一下采用配置文件的方式。 [python] view plain copy print?import loggingimport ()# 采用配置文件# create loggerlogger = (simpleExample)# application (debug message)(info message)(warn message)(error message)(critical message)采用了模式匹配的方式进行配置,正则表达式是r^[(.*)]$,从而匹配出所有的组件。 对于同一个组件具有多个实例的情况使用逗号‘,’进行分隔。 对于一个实例的配置采用componentName_instanceName配置块。 使用这种方式还是蛮简单的。 [plain] view plain copy print?[loggers]keys=root,simpleExample[handlers]keys=consoleHandler[formatters]keys=simpleFormatter[logger_root]level=DEBUGhandlers=consoleHandler[logger_simpleExample]level=DEBUGhandlers=consoleHandlerqualname=simpleExamplepropagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=在指定handler的配置时,class是具体的handler类的类名,可以是相对logging模块或是全路径类名,比如需要RotatingFileHandler,则class的值可以为:RotatingFileHandler或者。 args就是要传给这个类的构造方法的参数,就是一个元组,按照构造方法声明的参数的顺序。 输出:[plain] view plain copy print?2012-03-06 00:09:35,713 - simpleExample - DEBUG - debug message2012-03-06 00:09:35,713 - simpleExample - INFO - info message2012-03-06 00:09:35,714 - simpleExample - WARNING - warn message2012-03-06 00:09:35,714 - simpleExample - ERROR - error message2012-03-06 00:09:35,714 - simpleExample - CRITICAL - critical message这里还要明确一点,logger对象是有继承关系的,比如名为a.b和a.c的logger都是名为a的子logger,并且所有的logger对象都继承于root。 如果子对象没有添加handler等一些配置,会从父对象那继承。 这样就可以通过这种继承关系来复用配置。 3. 多模块使用logginglogging模块保证在同一个python解释器内,多次调用(log_name)都会返回同一个logger实例,即使是在多个模块的情况下。 所以典型的多模块场景下使用logging的方式是在main模块中配置logging,这个配置会作用于多个的子模块,然后在其他模块中直接通过getLogger获取Logger对象即可。 这里使用上面配置文件:[plain] view plain copy print?[loggers]keys=root,main[handlers]keys=consoleHandler,fileHandler[formatters]keys=fmt[logger_root]level=DEBUGhandlers=consoleHandler[logger_main]level=DEBUGqualname=mainhandlers=fileHandler[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=fmtargs=(,)[handler_fileHandler]class==DEBUGformatter=fmtargs=(,a,,5,)[formatter_fmt]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=主模块:[python] view plain copy print?import loggingimport ()root_logger = (root)root_(test root logger...)logger = (main)(test main logger)(start import module \mod\...)import (let\s test ())()root_(finish test...)子模块:[python] view plain copy print?import loggingimport submodlogger = ()(logger of mod say something...)def testLogger()(this is ...)()子子模块:[python] view plain copy print?import logginglogger = ()(logger of submod say something...)def tst()(this is ()...)然后运行python ,控制台输出:[plain] view plain copy print?2012-03-09 18:22:22,793 - root - DEBUG - test root logger...2012-03-09 18:22:22,793 - main - INFO - test main logger2012-03-09 18:22:22,809 - main - INFO - start import module mod...2012-03-09 18:22:22,809 - - INFO - logger of submod say something...2012-03-09 18:22:22,809 - - INFO - logger say something...2012-03-09 18:22:22,809 - main - DEBUG - lets test ()2012-03-09 18:22:22,825 - - DEBUG - this is ...2012-03-09 18:22:22,825 - - INFO - this is ()...2012-03-09 18:22:22,841 - root - INFO - finish test...可以看出,和预想的一样,然后在看一下,logger配置中的输出的目的地:[plain] view plain copy print?2012-03-09 18:22:22,793 - main - INFO - test main logger2012-03-09 18:22:22,809 - main - INFO - start import module mod...2012-03-09 18:22:22,809 - - INFO - logger of submod say something...2012-03-09 18:22:22,809 - - INFO - logger say something...2012-03-09 18:22:22,809 - main - DEBUG - lets test ()2012-03-09 18:22:22,825 - - DEBUG - this is ...2012-03-09 18:22:22,825 - - INFO - this is ()中没有root logger输出的信息,因为中配置了只有main logger及其子logger使用RotatingFileHandler,而root logger是输出到标准输出。

qq空间花匠学徒的分数是靠什么增加的?

以前的花和现在新开的花藤的成长是一样的,由四项属性值决定,即爱心值、阳光值、雨露值、营养值1.阳光值每1个访客(不包括未登录的访客)给这项指数增加1分,可以在当天为自己的空间 增加阳光指数3分。 2.爱心值自己每发表一篇日志(包括彩信日志)增加5分。 新增一个非QQ个性签名的心情增加3分,添加一首音乐只增加1分;相应地,您删除一篇日志扣5分,删除一个心情扣3分,删除一首音乐扣1分。 3.雨露值日志、心情和留言板每增加一个非自己的回复增加2分,阅读一个新小纸条增加2分。 每篇网络日志每被转载一次原创作者增加3分;删除日志评论及留言板留言不会扣除雨露,4.营养指数每在个人空间商城花费1元钱,给当前的植物的这一指数增加8分(手机消费除外),植物营养指数将在24小时内为您加上不过个人觉得现在的花藤更好看,我喜欢百合,我是5级的了,五朵,很漂亮

如何清空toncat的catalina.out后台日志

3.1日志类型与级别Tomcat日志分为下面5类:catalina、相当命令行输出日志localhost、相当于localhost主机的命令行输出日志manager、管理的日志admin、host-manager应该是虚拟主机方面每类日志的级别分为如下7种:SEVERE(highestvalue)>WARNING>INFO>CONFIG>FINE>FINER>FINEST(lowestvalue)3.2日志级别的设定方法tomcat每天都会在logs目录生成文件、、、、可以通过修改conf\文件来改变生成log的方式。示例:设定某类日志的级别(注:catalina日志会输出,不同于,对应于配置)

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐