Could not load file or assembly ' log4net, version=1.2.10.0, culture=neutral, publickeytoken=692fbea5521e1304 ' or one of it s dependencies. The system could not find the specified file.
This problem has recently been encountered because of the log4net problem.
The first step: Download log4net
Download Address: http://logging.apache.org/log4net/download_log4net.cgi
After extracting the downloaded Log4net-1.2.11-bin-newkey, the following figure shows:
Double-click the Bin folder
Double-click the Net Folder to select a different version for. NET Framerwork
Find the appropriate version of Log4net.dll
Step Two: Apply Log4net
1, add Log4Net.DLL references in the project
Development environment: XP Professional Service Pack3
Development tools: VS2010 Flagship edition
Create a new WinForm application, the effect is as follows only for a simple demo
The form is named: FormMain
Copy the Log4Net.Dll to the root of the project, and then add the Log4net reference
After adding a reference
2. Configure Log4net
(1) Create a new application configuration file app.config
(2) The contents of the app.config configuration file are as follows
1 <?xml version= "1.0"?> 2 <configuration> 3 <configSections> 4 <section name= "log4net" type = "Log4net." Config.log4netconfigurationsectionhandler, log4net "/> 5 </configSections> 6 <log4net> 7 <!--set The semantic output to the file is--> 8 <appender name= "Rollinglogfileappender type=" log4net. Appender.rollingfileappender "> 9 <!--definition file storage location--> <file value=" log\\ "/> <appendt Ofile value= "true"/> <rollingstyle value= "Date"/> <datepattern value= "Yyyy\\yyyymm\\yyyymmdd ' txt ' '/> <staticlogfilename value= ' false '/> <param name= ' maxsizerollbackups ' value=
T <layout type= "log4net. Layout.patternlayout "> <!--text description at the end of each log--> <!--output format--> <!--sample: 2008-03-26 13:42:32,111 [[] INFO Log4netdemo.mainclass [(null)]-info--> <conversionpattern value= "%newline%n Records Time:%date%n thread Id:[%thrEAD]%n Log level:%-5level%n Error Class:%logger property: [%property{ndc}]-%n error description:%message%newline%n "/> </layout>
; </appender> <root> <level value= "ERROR"/> <!--file record log--> 26 <appender-ref ref= "Rollinglogfileappender"/> </root> </log4net> </configuration>
For each parameter, refer to the following link:
Log4net use detailed reprint Zhou Gong
http://blog.csdn.net/zhoufoxcn/article/details/2220533
Log4net the use of detailed and continued to reprint Zhou Gong
Http://www.cnblogs.com/zhoufoxcn/archive/2010/11/23/2515616.html
(3) Create a new Loghelper help class that is invoked in multiple forms, and the class contents are as follows:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text; 5 6 [Assembly:log4net. Config.xmlconfigurator (Watch = True)] 7 namespace Testlog4net 8 {9 public class Loghelper 10 {11/// <summary> 12///output log to log4net///</summary>///<param name= "T" ></para m>///<param name= "Ex" ></param> #region static void Writelog (Type T, Exception ex) 1 7 public static void Writelog (Type T, Exception Ex) {log4net. ILog log = log4net.
Logmanager.getlogger (t); Log.
Error ("Error", ex); #endregion///<summary> 27///output log to log4net///</ summary>///<param name= "T" ></param>///<param "msg" ></param> 31 #region static void Writelog (Type T, string msg) public Static void Writelog (Type T, string msg) {log4net. ILog log = log4net.
Logmanager.getlogger (t); Log.
Error (msg); #endregion 40 41 42} 43}
When you build the solution, you may receive the following error message:
Could not find type or namespace name ' log4net ' (using directive or assembly reference is missing?) Solving method
The name "log4net" workaround does not exist in the current context
At this point may be some people wonder, obviously added a reference to how also prompted to find the namespace. To solve this problem is very simple, right key item select attribute
Modifying the target framework default. NET Framework4 Client profile to. NET Framework4 and then rebuilding the solution is OK.
Step Three: Test log4net
Add the following code to the button's event
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
namespace Testlog4net one
{of
public partial class Formmain:form Public FormMain () InitializeComponent (); the private void btnTest_Click (object sender, EventArgs e) / / /(1) FormMain is the class name// (2) The second parameter is the string information Loghelper.writelog (typeof ( FormMain), "Test log4net log is written"); (1) FormMain is a class name //(2) The second parameter is an exception block that needs to be caught
30 //try { //}catch (Exception ex) {MB // Loghelper.writelog ( typeof (FormMain), ex);
41} per//
}
Run the Item click button and you will see the log folder in the project's root directory
When you open the log folder, you'll see the folder with the date name open and you can see the following image
Seeing this document shows that Log4net.dll's application has been successful.
Note: The name of the folder is automatically generated according to the file node you set up in app.config, you can set the path and format for yourself according to your own needs.
If part of the picture does not appear, please use the Chrome browser to view.
Download log4net Test Source