Log4net logging, detailed configuration (for your own use), and log4net Logging
[From Baidu encyclopedia] The log4net library is based on the Apache log4j framework in Microsoft. NET platform is a tool that helps programmers output log information to various targets (console, files, databases, etc.
1. First Add a reference to log4net. dll. You can download nuget from [http://logging.apache.org/log4net/download_log4net.cgi #
2. Configure in web. config (App. config)
1 View Code
2.1. This is the m
Log4net independent Configuration File Settings, log4net configuration file
1. Create a configuration file log4net. config in the same way as web. config or app. config;
2. Create related class files as follows:
Public class LogHelper{Private LogHelper (){SetConfig ();}
Private static readonly log4net. ILog loginfo = LogManager. GetLogger ("myLogger ");
Private static bool IsLoadConfig = false;Private static void SetConfig (){XmlConfi
the project:
1) When an error is reported, you can record the controller and method of the error code, as well as the request parameters and time when the error is reported;
2) return JSON in a specific format to facilitate front-end processing. Because most of the system's ajax requests are now, an error is returned and the default error page of MVC is returned, which is not easy to handle at the front end.
Create a new class LogExceptionAttribute to inherit HandleErrorAttribute and override t
code:Org.apache.log4j.Logger Log4jlogger;if (Name.equalsignorecase (logger.root_logger_name))Log4jlogger = Logmanager.getrootlogger ();ElseLog4jlogger = Logmanager.getlogger (name);Logger newinstance = new Log4jloggeradapter (Log4jlogger);2.1 We can see the native way through the log4j1, that is, using the log4j1 Logmanager to obtain, the introduction of the log4j1 load configuration file, and then initialize,Finally return to a Org.apache.log4j.Logg
that are passed in externally. HttpContext is used to write a Response to Response, and AsyncCallBack is used to end the current Http persistent connection request, I didn't use ExtraData for what I should do. Note that the CompletedSynchronously attribute in this class should return false, otherwise the client will not receive the data. In addition, do not return null for each attribute. Otherwise, a null pointer error will be reported when Response is written.
Next we will look at the impleme
1. First, reference log4net. dll in the project, and then add a configuration file log4net. config in the project.
2. add a sentence to AssemblyInfo. cs in the Properties folder of the project.
Of course, you must first add using log4net; and then add [assembly: XmlConfigurator (ConfigFile = "log4net. config", Watch = true)]
3. Start to use it where logs need to be recorded.
First, add using log4net;
Then define the global log variable private static readonly ILog Log =
Configuration
Copy codeThe Code is as follows:
There are two more log4net nodes.
Copy codeThe Code is as follows:
In the framework system, all log objects are descendants of root logger. Therefore, if a log object is not explicitly defined in the configuration file, the framework uses the attributes defined in the root log. In the Then there will be Database Log writing and email writing.Initialize configuration in the Global. asax File
Copy codeThe Code is as follows:Protected void Application
Log4net entry (HELP) and log4net entry help
In the previous sections of the Log4net getting started file, we need to write the following code in the log4net class:
1 private static log4net. ILog log = log4net. LogManager. GetLogger (System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType );
If multiple classes use log4net to output log information, write this line of code in each class. To avoid this situation, we will write a static class
control (read configuration files and other operations) SendMessage sm = new SendMessage (); // set the parameter sm. arg1 = "1"; sm. arg2 = "2"; sm. arg3 = "3"; sm. arg4 = "4"; // execution method, returns var result = sm. send (); Console. writeLine (result );}
However, if SendMessage is written in this way
Public class SendMessage {ILog Log; public SendMessage () {Log = log4net. logManager. getLogger (typeof (SendMessage); Log. info ("initializati
in the simple button. The Code is as follows:
Ischedulerfactory Sf = new stdschedulerfactory (); isched1_sched = SF. getscheduler (); jobdetail job = new jobdetail ("job2", "Group2", typeof (jobexecute_2); simpletrigger trigger = new simpletrigger ("trigger2", "Group2 "); // The trigger name cannot be the same for different scheduled tasks! Trigger. starttime = datetime. now. addseconds (5); trigger. repeatinterval = 5000; trigger. repeatcount = 1000; datetime Ft = sched. schedulejob (job, tri
without painting)
Session (also called an interaction)
The long rectangle on the line of the life cycle represents a session (or interaction). You can use vs reverse engineering to generate a sequence chart and you will find that the frmlogin object on the UI Layer calls the BLL layer: when a logmanager object is called, the bars between two methods (testlogin and addworklog) are broken, not consecutive. Why? Because every round-trip call from the U
. formatter
Each handler output can be configured with a formatter.
Existing formatter:
Simpleformatter orchestrates all log information in text format
Xmlformatter generates log information in XML format
You can also customize formatter. In the following example, formatter can encapsulate log information in HTML format:
package logging;import java.text.SimpleDateFormat;import java.util.Date;import java.util.logging.Formatter;import java.util.logging.Handler;import java.util.logging.Level;i
/// Log record
Public static class childllylog{Static ilog log = logmanager. getlogger (typeof (childllylog ));Public static void log (string logtitle){Log. Info (logtitle );
}Public static void error (string logtitle, exception ex){Log. Error (logtitle, ex );
}}
/// Construct a job Template
Public sealed class etsjobinfo { /// /// Initialize the constructor. This job template mainly implements Server Ping and telnet based on differen
C # obtain the remote image. The authorization authentication of the form user name and password is required.
Using system; using system. collections. generic; using system. drawing; using system. io; using system. LINQ; using system. net; using system. text; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace web. app_code {public partial class getfleximage: system. web. UI. page {public static readonly log4net. ilog log = log4net.
First download log4net (nonsense ...)1. Add log4net reference2. Create a configuration file under the winform program. The default configuration file is app. config. The changes are as follows:3-20 rows are newly addedConfiguration File
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22
23
3. After adding, add the following to assemblyinfo. CS:[Assembly: log4net. config. domconfigurator (conf
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.