Log4net (ASP. net mvc 5), log4netmvc

Source: Internet
Author: User

Log4net (ASP. net mvc 5), log4netmvc

In the first four Log4net introductory articles, we talked about some simple usage of log4net. In this article, we mainly talked about how to use it in ASP.. net mvc 5 Project writes log information to the SQL Server database.

1. Create the simplest ASP. net mvc 5 Application

1. Open VS2015, click File> new project to create an ASP.. NET Web application, name the project "Log4netMVC", and click "OK.

2. In the "select template" option, select the "Empty" template and select the "MVC" option in the "add folder and core reference for the following items" option, click "OK.

3. Right-click the "Controllers" folder in the project, click "add"> "controller", select "MVC 5 controller-null", and click "add. In the displayed dialog box, name the controller "HomeController" and click "add.

4. Double-click the newly added "HomeController. cs file, locate the Index () method, right-click the method name, and click the add view option. The add view dialog box is displayed, in this dialog box, deselect the "use layout page" option. Other options remain unchanged, and then click "add.

5. Add the following code to the div element in the Index. cshtml file:

1 @ {2 Layout = null; 3} 4 5 <! DOCTYPE html> 6 7 

So far, the simplest ASP. net mvc 5 project has been created. The project structure is as follows:

2. Configure log4net in ASP. net mvc 5

1. Choose tools> NuGet Package Manager> NuGet package management. Click the Browse tab and enter log4net in the search box ", press enter, click "log4net", select "Log4netMVC" on the right, select "latest stable version 2.0.5", and click "Install". Wait until the installation is complete.

2. Expand the "Properties" node in the Log4netMVC project, double-click the "AssemblyInfo. cs" file, and add the following code at the end of the file:

1 [assembly: log4net. Config. XmlConfigurator (ConfigFile = "Log4net. config", Watch = true)]

3. Add a configuration file named "Log4net. config" under the root directory of the Log4netMVC project. Note that the name of the configuration file is consistent with the value of "ConfigFile" in step 1. Configure the Log4net. config file as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 5 </configSections> 6 7 <system. web> 8 <compilation debug = "true" targetFramework = "4.5.2"/> 9 

4. Double-click the Global. asax file under the root directory of Log4netMVC and add a line of code in the Application_Start () method. The final code is as follows:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Routing; 7  8 namespace Log4netMVC 9 {10     public class MvcApplication : System.Web.HttpApplication11     {12         protected void Application_Start()13         {14             AreaRegistration.RegisterAllAreas();15             RouteConfig.RegisterRoutes(RouteTable.Routes);16 17             log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("Log4net.config")));18         }19     }20 }

5. Double-click the "HomeController. cs" file and edit the Code as follows:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6  7 namespace Log4netMVC.Controllers 8 { 9     public class HomeController : Controller10     {11         private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);12 13         // GET: Home14         public ActionResult Index()15         {16             log.Info("ASP.NET MVC Info!");17             return View();18         }19     }20 }

3. Create a database and a log table

First, create a database named "Management" in SQL Server, and then create a table named "Log" in the database. The table structure is as follows:

 1 CREATE TABLE [dbo].[Log]( 2     [Id] [int] IDENTITY(1,1) NOT NULL, 3     [Date] [datetime] NOT NULL, 4     [Thread] [varchar](255) NOT NULL, 5     [Level] [varchar](50) NOT NULL, 6     [Logger] [varchar](255) NOT NULL, 7     [Message] [varchar](4000) NOT NULL, 8     [Exception] [varchar](2000) NULL, 9  CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED 10 (11     [Id] ASC12 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]13 ) ON [PRIMARY]

Now, all the preparation and configuration are complete. Run the ASP. net mvc 5 Application and you can see that the log is successfully written to the SQL Server database.

Source code download

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.