调教类小游戏,具体而微造句,灰色空间简谱
今天自己要弄一个日志记录功能,以前也弄过 但是都忘了,今天又弄了一下 花了几十分钟,在此记录一下
第一步:添加log4net.dll
第二步:配置 示例如下: 我是直接配置在了web.config下
<?xml version="1.0" encoding="utf-8"?> <configuration> <configsections> <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net" /> </configsections> <log4net> <logger name="student"> <level value="all" /> <appender-ref ref="rollingfile" /> </logger> <appender name="rollingfile" type="log4net.appender.rollingfileappender,log4net"> <file value="log/" /> <datepattern value="yyyy-mm-dd".txt"" /> <staticlogfilename value="false" /> <maxsizerollbackups value="-1" /> <rollingstyle value="date" /> <appendtofile value="false" /> <maximumfilesize value="1024mb" /> <layout type="log4net.layout.patternlayout,log4net"> <conversionpattern value="%-38m %-7p %-20d %n" /> </layout> </appender> </log4net> <!--下面的不是!!!--> <appsettings> <add key="webpages:version" value="3.0.0.0" /> <add key="webpages:enabled" value="false" /> <add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" /> </appsettings> <system.web> <compilation debug="true" targetframework="4.7.2" /> <httpruntime targetframework="4.7.2" /> </system.web> </configuration>
第三步:在global.asax.cs文件下添加 log4net.config.xmlconfigurator.configure(); 如下:
public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); log4net.config.xmlconfigurator.configure(); } }
第四步:添加log帮助类 然后使用即可 这里需要注意的是名字对应 看我的下面代码中的注释
public class loghelper { //student是你的配置文件 <logger name="student"> 的name的值 private static log4net.ilog log = log4net.logmanager.getlogger("student"); public static void debug(object message, exception e) { log.debug(message, e); } public static void debug(object message) { log.debug(message); } public static void info(object message) { log.info(message); } public static void warn(object message) { log.warn(message); } public static void error(object message) { log.error(message); } public static void error(object message, exception e) { log.error(message, e); } public static void log(object message) { log.info(message); } }
配置文件的内容比较简略 详细的自己百度看看其他比较详细的即可
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论