In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "C# how to use Log4.net to record log files", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "C# how to use Log4.net to record log files" bar!
1. Brief introduction 1.1 advantages of Log4.net
Almost all large applications have their own API for tracking and debugging. Because once the program is deployed, it is no longer possible to use specialized debugging tools. However, an administrator may need to have a powerful logging system to diagnose and fix configuration problems. Experience has shown that logging is often an important part of the software development cycle. It has the following advantages:
1. It can provide a precise environment for the application to run, so that developers can find the Bug in the application as soon as possible.
two。 Once the Log output code is added to the program, log information can be generated and output while the program is running without human intervention.
3. In addition, the log information can be output to different places (console, files, etc.) for future research.
4.Log4net is designed for this purpose as a logging package for the .NET development environment.
1.2 Log4.net structure
Log4net has four main components, namely, Logger (logger), Repository (library), Appender (attachment), and Layout (layout).
1.3 logging method
1.AdoNetAppender: logs recorded to the database using ADO.NET.
2.AnsiColorTerminalAppender: write high-brightness log events at the ANSI window terminal.
3.AspNetTraceAppender: you can view recorded logs as Trace in asp.net.
4.BufferingForwardingAppender: cache log events before outputting to a child Appenders.
5.ConsoleAppender: outputs the log to the console.
6.EventLogAppender: write the log to Windows Event Log.
7.FileAppender: writes the log to a file.
8.LocalSyslogAppender: write logs to local syslog service (for UNIX environments only).
9.MemoryAppender: save the log to a memory buffer.
10.NetSendAppender: output the log to Windows Messenger service. This log information will be displayed in the dialog box of the user terminal.
11.RemoteSyslogAppender: logs are written to Remote syslog service through the UDP network protocol.
12.RemotingAppender: logs are written to the remote receiver through the .NET Remoting.
13.RollingFileAppender: writes the log to a file as a rollback file.
14.SmtpAppender: write the log to the email.
15.TraceAppender: writes logs to the .NET trace system.
16.UdpAppender: sends the log connectionless UDP datagrams to the remote host or broadcasts it as UdpClient.
1.4 parameters
1% m (message): output log messages, such as ILog.Debug (...) An output message
2% n (new line): line feeds
3% d (datetime): outputs the time the current statement is running
4% r (run time): output the number of milliseconds consumed by the program from run to execution to the current statement
5% t (thread id): ID of the thread in which the current statement resides
6% p (priority): the current priority of the log, that is, DEBUG, INFO, WARN... Etc.
7% c (class): the name of the current log object, for example:
8% f (file): the file name where the output statement is located.
9% l (line): the line number on which the output statement is located.
10.% number: represents the minimum length of the item. If it is not enough, fill it with spaces. For example, "%-5level" means that the minimum width of level is 5 characters, and if the actual length is less than 5 characters, fill with spaces.
II. Define configuration files
1. To use the log4.net logging function, add the following statement to the AssemblyInfo.cs file in the Web or Winform program, which means that log4net automatically looks for the configuration file log4net.config to obtain and load the configuration information. Note that the ConfigFile here is consistent with the .Config file name in the project. The name of this configuration file defaults to App.config or Web.config under the created project.
ConfigFile should be the same as the .Config file name in the project
[assembly: log4net.Config.XmlConfigurator (ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
two。 To use log4net, first add the configuration to the node in the config file (manually if you don't have this node), as follows:
3. In addition, add child nodes under the top-level node. Under the node, you can add child nodes, each of which represents a way to log (just because it is configured here does not mean it is enabled).
4. Two points for attention
First: in the click configuration file, change the following figure ITEM in the properties of the log4.config file to: assign to the output directory-> copy if newer.
4.2 second: in the configuration file above, and
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.