In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "the use of software in asp.net log4net", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Software in asp.net using log4net" this article.
This program demonstrates how to use log4net to record program log information. Log4net is a well-known open source logging component. Using log4net, you can easily record log information to files, console, Windows event logs and databases (including MS SQL Server, Access, Oracle9i,Oracle8i,DB2,SQLite). And we can also record and control the log level to be logged. The log categories that can be recorded include: FATAL (fatal error), ERROR (general error), WARN (warning), INFO (general information), DEBUG (debugging information). To get the latest version of the log4net component library, you can download it from the official website http://logging.apache.org/log4net/. The latest version is 1.2.10.
The following example shows how to log using log4net.
First download the latest version of the log4net component from the official website, and the latest version is 1.2.10. In the program, we only need the log4net.dll file on it, add a reference to log4net.dll, and we can use it in the program.
Log4net.dll download address: https://www.yisu.com/dll/Log4net.dll.html
Then we configure the relevant configuration files (WinForm corresponds to * .exe.config and WebForm corresponds to * .config). In this example, the console application is configured as follows (with instructions for each configuration):
< configuration > < configSections > < section name ="log4net" type ="System.Configuration.IgnoreSectionHandler" /> < appSettings > < log4net > < appender name ="LogFileAppender" type ="log4net.Appender.FileAppender" > < file value ="D:/log4netfile.txt" /> < appendToFile value ="true" /> < rollingStyle value ="Date" /> < datePattern value ="yyyyMMdd-HH:mm:ss" /> < layout type ="log4net.Layout.PatternLayout" > < footer value ="by 周公" /> < conversionPattern value ="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> < appender name ="ConsoleAppender" type ="log4net.Appender.ConsoleAppender" > < layout type ="log4net.Layout.PatternLayout" > < conversionPattern value ="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> < appender name ="EventLogAppender" type ="log4net.Appender.EventLogAppender" > < layout type ="log4net.Layout.PatternLayout" > < conversionPattern value ="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> < appender name ="AdoNetAppender_Access" type ="log4net.Appender.AdoNetAppender" > < connectionString value ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:log4net.mdb" /> < commandText value ="INSERT INTO LogDetails ([LogDate],[Thread],[Level],[Logger],[Message]) VALUES (@logDate, @thread, @logLevel, @logger,@message)" /> < parameter > < parameterName value ="@logDate" /> < dbType value ="String" /> < size value ="240" /> < layout type ="log4net.Layout.PatternLayout" > < conversionPattern value ="%date" /> < parameter > < parameterName value ="@thread" /> < dbType value ="String" /> < size value ="240" /> < layout type ="log4net.Layout.PatternLayout" > < conversionPattern value ="%thread" /> < parameter > < parameterName value ="@logLevel" /> < dbType value ="String" /> < size value ="240" /> < layout type ="log4net.Layout.PatternLayout" > < conversionPattern value ="%level" /> < parameter > < parameterName value ="@logger" /> < dbType value ="String" /> < size value ="240" /> < layout type ="log4net.Layout.PatternLayout" > < conversionPattern value ="%logger" /> < parameter > < parameterName value ="@message" /> < dbType value ="String" /> < size value ="240" /> < layout type ="log4net.Layout.PatternLayout" > < conversionPattern value ="%message" /> < root > < appender-ref ref ="LogFileAppender" /> < appender-ref ref ="ConsoleAppender" /> < appender-ref ref ="EventLogAppender" />Program file:
Using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Reflection;using log4net / / Note that the following statement must be added, specifying that log4net uses a .config file to read configuration information / / if it is WinForm (assuming the program is MyDemo.exe, a MyDemo.exe.config file is required) / / if it is WebForm Then read the relevant information from web.config [assembly: log4net.Config.XmlConfigurator (Watch = true)] namespace Log4NetDemo {/ description: this program demonstrates how to use log4net to record program log information. Log4net is a well-known open source logging component. / / using log4net, you can easily record log information to files, console, Windows event logs and databases (including MS SQL Server, Access, Oracle9i,Oracle8i,DB2,SQLite). / / the following example shows how to log using log4net / / author: Duke Zhou / / time: 2008-3-26 / initial address: http://blog.csdn.net/zhoufoxcn/archive/2008/03/26/2220533.aspx / public class MainClass {public static void Main (string [] args) { / / Application.Run (new MainForm ()) / / create logging component instance ILog log = log4net.LogManager.GetLogger (MethodBase.GetCurrentMethod () .DeclaringType); / / record error log log.Error ("error", new Exception ("an exception occurred")) / / record critical error log.Fatal ("fatal", new Exception ("fatal error occurred"); / / record general information log.Info ("info"); / / record debugging information log.Debug ("debug") / / record warning message log.Warn ("warn"); Console.WriteLine ("logging completed. "); Console.Read ();}
Running result:
Output on the console
Contents of log file
Note that there is a sentence in the above code: [assembly: log4net.Config.XmlConfigurator (Watch = true)] (at the namespace of the class that needs to use log4net). If you don't have this sentence, you will get the situation of "isDebugEnable when the program is debugged" in the following message.
The above is all the contents of the article "Software using log4net in asp.net". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.