Log4net and general logging methods

Source: Internet
Author: User
Tags log4net

Download

Http://files.cnblogs.com/crazyair/log4net.zip

1 Create a new log4net.config in a Web project

<?xml version="1.0"encoding="Utf-8"?><!--Note: In addition to manually editing this file, you can also use the WEB management tool to configure settings for your application. You can use the Web site in Visual Studio-the "ASP. NET Configuration" option. A complete list of settings and comments in Machine.config.comments, the file is typically located in the/windows/microsoft.net/framework/v2.x/Config in--><configuration> <configSections> <section name="log4net"Type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <logger name="Ikangdentallog"> <!--control log level:all| debug|info| warn| error| fatal| Off--> <!--If the value of level is not defined, the default is debug--> <level value=" All"/> <appender-ref ref="Smtpappenderax"></appender-ref> <appender-ref ref="Fileappenderax"></appender-ref> </logger> <appender name="Smtpappenderax"Type="log4net. Appender.smtpappender"> <to value="[email protected]"></to> < fromValue="[email protected]"/> <subject value="AX ' Test Log Message"/> <smtphost value="mail.eshinfo.com"/> <username value="Eshinfo"/> <password value="Eshinfo"/> <buffersize value="2048"/> <!--Extra long part is discarded--<lossy value="false"/> <!--output levels between warn and off log--and <filter type="log4net. Filter.levelrangefilter"> <param name="Levelmin"Value="WARN"/> <param name="Levelmax"Value="OFF"/> </filter> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%newline%date [%thread]%-5level%logger [%PROPERTY{NDC}]:%NewLine%message%newline"/></layout> </appender> <appender name="Fileappenderax"Type="log4net. Appender.rollingfileappender"> <!--absolute path--<file value="D://ax.txt"></file> <!--relative path, at the root of the project-<!--with the last path, so the absolute path above does not write the log--<file value="./log/ax.txt"></file> <!--by day-<appendtofile value="true"/> <rollingstyle value="Date"/> <!--<datepattern value="yyyymmdd-hhmm"/>--> <datepattern value="YYYYMMDD"/> <!--prevent multiple threads from writing log, the official says thread is not secure--<!--is actually used, local tests are normal, there is no log after deployment--<lockingmodel type="log4net. Appender.fileappender+minimallock"/> <appendtofile value="true"/> <!--can be: once| size| date| Composite--> <!--Composite is a combination of size and date--<rollingstyle value="Composite"/> <!--log maximum number, is the latest---<!--when the Rollingstyle node is date, the node does not work-<!--rollingstyle node is size, only VA Lue Log--<!--the Rollingstyle node is composite, there is a value log per day---<maxsizerollbackups value="Ten"/> <!--When you back up a file, add a suffix--<!--suffix to *.txt, for example: ax.txt_2008- -- -. PxP should be a bug--> <!--suffix * on the program. TXT, example: ax.txt_2008- -- -. Txt--> <datepattern value="_yyyy-mm-dd.txt"/> <!--available units: kb| mb| Gb--> <!--do not use decimals, otherwise it will always be written to the current log--<maximumfilesize value="1MB"/> <!--set to True, the current most recent log file name is always the name of the file section---<staticlogfilename value="true"/> <!--output level logs between info and error--<filter type="log4net. Filter.levelrangefilter"> <param name="Levelmin"Value="INFO"/> <param name="Levelmax"Value="ERROR"/> </filter> <!--must be combined, the first one only filters out warn, the second rejects other log output--<filter type="log4net. Filter.levelmatchfilter"> <param name="Leveltomatch"Value="WARN"/> </filter> <filter type="log4net. Filter.denyallfilter"/> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%date [%thread]%-5level%logger [%NDC]-%message%newline"/> </layout> </appender> </log4net></configuration>

Join in AssemblyInfo.cs in class library common

[Assembly:log4net. Config.xmlconfigurator (configfile = "Log4net.config", Watch = True)]

To create a new class

public class Log {private static log4net. ILog log = log4net.        Logmanager.getlogger ("Ikangdentallog"); public static void Debug (String message) {if (log. isdebugenabled) {log.            Debug (message); }} public static void Debug (System.Exception ex1) {if (log. isdebugenabled) {log. Debug (Ex1. Message.tostring () + "/r/n" + Ex1. Source.tostring () + "/r/n" +ex1. Targetsite.tostring () + "/r/n" + Ex1.            Stacktrace.tostring ()); }} public static void Error (String message) {if (log. iserrorenabled) {log.            Error (message); }} public static void Fatal (String message) {if (log. isfatalenabled) {log.            Fatal (message); }} public static void Info (String message) {if (log.            isinfoenabled) {    Log.            Info (message); }} public static void Warn (String message) {if (log. iswarnenabled) {log.            Warn (message); }        }    }

Reference log4net

When using the Common.Log.Info ("1111");

2 A simple logging

Public  class log    {        ///<summary>///write log (for tracking)///        </summary> public       static void Writelog (String Strmemo)        {            string filename = HttpContext.Current.Request.PhysicalApplicationPath;            Saved path            string time = DateTime.Now.ToString ("Yyyy-mm-dd");            filename = filename + "/log/" + "+" "+ Time +". txt ";            StreamWriter sr = null;            Try            {                if (! File.exists (filename))                {                    sr = file.createtext (filename);                }                Else                {                    sr = file.appendtext (filename);                }                Sr. WriteLine (Strmemo);            }            Catch            {            }            finally            {                if (sr! = NULL)                    Sr. Close ();}}    }

Use the time Log.writelog ("XXX");

Download Http://files.cnblogs.com/crazyair/log4net.zip

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.