Log4net (WCF) and log4net (wcf)

Source: Internet
Author: User
Tags log4net

Log4net (WCF) and log4net (wcf)

In the last Log4net entry (ASP. net mvc 5), we described how to use log4net in the ASP. net mvc 5 project. In this article, we will describe how to use log4net in a WCF application. To illustrate this process, we will create three projects: WCF Service Library Project, WCF Service application, and client application. The WCF Service library project is mainly used to compile our WCF contracts and services. the WCF Service Application is mainly used to host our WCF Service library on IIS, the main purpose of the client application is to call the WCF Service hosted on IIS.

I. Project Creation

1. Create a blank solution: Start VS2015, click File> New> project. In the Create Project dialog box, expand the "other project type" node and select the "Visual Studio" solution. We will create a blank solution named "Log4netWCF.

2. Create a WCF Service Library Project: Right-click the "Log4netWCF" solution created in step 1, and choose "add"> "new" project, in the "Add new project" dialog box, select the "WCF" node, select the "WCF Service library" project on the right, and name the project "Log4netWCFServiceLibrary ", click "OK" to create a simple WCF Service. IService1.cs is the service contract and Service1 is the implementation of the contract. This is the Demo code provided by Microsoft, for the sake of simplicity, We will directly use this simplest service without making any changes.

3. Create a WCF Service Application: Right-click the "Log4netWCF" solution created in step 1, and click "add"-"New" project, in the "new project" dialog box, select the "WCF" node, select the "WCF Service Application" project on the right, and name the project "Log4netWCFService ", click "OK" to create the WCF Service Application.

4. In the newly created "Log4netWCFService" project, right-click the "IService1.cs" file and choose to delete it. Expand Service1.svc, right-click "Service1.svc. cs", and select Delete.

5. Right-click the "Reference" node of the "Log4netWCFService" project and select the "add reference" option. In the "reference manager" dialog box that appears, expand the "project" node, select the "solution" node, select "Log4netWCFServiceLibrary" on the right, and click "OK. In this way, we add a reference to the "Log4netWCFService" service library in the "Log4netWCFServiceLibrary" service application project.

6. Double-click to open the Service1.svc file in the "Log4netWCFService" project and change the code to the following code:

1 <%@ ServiceHost Language="C#" Debug="true" Service="Log4netWCFServiceLibrary.Service1" CodeBehind="Service1.cs" %>

7. Right-click the "Log4netWCF" solution and click the "generate solution" option to compile the entire solution. If there are no errors, continue with the following operations.

8. Right-click the "Log4netWCFService" project and click the "publish" option to bring up the "publish Web" dialog box, as shown below:

9. Select the "Custom" option and enter the configuration file name "Log4netWCF". You can take this name and remember it. Click "OK" to go to the next step, as shown in:

10. Select "File System" in "Publish method:", as shown in:

11. Click the "..." button after "Target location:" to bring up the "Target location" dialog box. In this dialog box, click "Local IIS", as shown in:

12. In, click "Default Web Site" under "IIS website", and then click "create new Web application" in the upper right corner, as shown in:

13. Enter the application name "Log4netWCF", as shown in:

14. Click "open", return to the "publish Web" dialog box, and click "Next. On the "Settings" tab, expand "File Publish Options" and select "Delete all existing files prior to publish" and "Precompile during publishing", as shown in:

15. Click "Next" and then click "release". Wait until the message is displayed.

16. Open the control panel, choose system and security> Administrative Tools, and run "Internet Information Service (IIS) manager" as an administrator ", expand the "Default Web Site" node in the "Internet Information Service (IIS) manager" window, select the "Log4netWCF" application, and click "content View" at the bottom right of the application ", right-click the "Service1.svc" file and choose browse. If the WCF Service is not incorrect, the following page is displayed:

Note: The service address in: http: // localhost/Log4netWCF/Service1.svc will be used to add service references in the client program.

17. Create a client application: For convenience, create a client application as a console application and right-click the "Log4netWCF" solution created in step 1, click "add"> "New Project", select "console application" in the "Add new project" dialog box, and name it "Client, click "OK.

18. In the new "Client" project, right-click the "Reference" node and click the "add service reference" option, in the address bar of the "add service reference" dialog box that appears, enter the service address generated in Step 1: http: // localhost/Log4netWCF/Service1.svc, and click "go, change the namespace to "Log4netWCF", as shown in:

19. Click "OK" to reference the WCF Service.

20. Double-click the "Program. cs" file in the "Client" project and modify the Code as follows:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Client 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             Log4netWCF.Service1Client proxy = new Log4netWCF.Service1Client();14             proxy.Open();15             string returnString = proxy.GetData(7);16             proxy.Close();17 18             Console.WriteLine(returnString);19             Console.ReadKey();20         }21     }22 }

21. Set the "Client" project to start the project, and then run the program. If there are no errors, the following result page is displayed:

So far, all our WCF applications have been created. Next, we will add log4net usage in the WCF application.

2. Configure log4net in a WCF Application

1. Click Tools> NuGet Package Manager> Manage NuGet packages in the VS toolbar. On the "NuGet-solution" tab, click the "Browse" tab, enter "log4net" in the search box, select "log4net" in the search result, select "Log4netWCFServiceLibrary" on the right, and select the latest stable version, click "Install", as shown in:

2. After installing log4net, click the Properties node in the "Log4netWCFServiceLibrary" 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. Double-click "Service1.cs" in the "Log4netWCFServiceLibrary" project and modify the Code as follows:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. runtime. serialization; 5 using System. serviceModel; 6 using System. text; 7 8 namespace Log4netWCFServiceLibrary 9 {10 // Note: use the "RENAME" command on the "refactoring" menu to change the class name "Service1" in the Code and configuration file at the same time ". 11 public class Service1: IService112 {13Private static log4net. ILog log = log4net. LogManager. GetLogger (System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType );14 15 public string GetData (int value) 16 {17Log. Debug ("debug! ");18Log. Info ("info! ");19Log. Warn ("warn! ");20Log. Error ("error! ");21Log. Fatal ("fatal! ");22 23 return string. format ("You entered: {0}", value); 24} 25 26 public CompositeType GetDataUsingDataContract (CompositeType composite) 27 {28 if (composite = null) 29 {30 throw new ArgumentNullException ("composite"); 31} 32 if (composite. boolValue) 33 {34 composite. stringValue + = "Suffix"; 35} 36 return composite; 37} 38} 39}

4. In the "Log4netWCFService" project, add a project named "Log4net. in the config file, we configure to split the log file by date and use the date as part of the log file name. The specific configuration file is 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 

5. re-release the "Log4netWCFService" project to IIS. When you re-release the project, click the "publish" menu and then click the "publish" button.

6. Run the "Client" console application. After running the application, you can view the log file "WCFLogs_20161215.log" in the C: \ Logs \ directory, which may have different dates.

So far, how to use log4net In the WCF application will be discussed here, and we wish you a smooth implementation!

Source code download

Related Article

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.