Another exception occurs when the xxx type initial value is set"

Source: Internet
Author: User
Tags log4net

A lightweight open-source IOC project implemented by C # is implemented. However, when it is used locally, it is found that"Type Initial Value Setting raises an exception"Exception information, debugging several times still cannot determine where the problem has occurred. It seems that I have encountered such an exception. It is estimated that the initialization of a static class is unsuccessful, but it is not certain.

Instead, open ilspy to view the source code and find that there are many static initialization tasks.CodeIt looks like layers of nesting, and there are many classes, so it is easy to get dizzy when one row is followed up. After a long time of failure, I had no choice but to google it. I found that some people had encountered the same problem as I did. I almost did not go crazy when I saw the correct solution with the most votes: directly in the project.Add log4net. dll referenceYou can.

After the test, sure enough.

Ah ah...

It turned out to be log4net, and it turned out to be log4net.

According to my personal experience, many open-source projects use log4net to record logs, for example, I have encountered this problem when using a client implemented by the C # client of ibatis.net, postsharp, fastdfs, And the C # client of memcached.

Can I quickly find the cause of the same problem next time? Can we stop wasting time? Can we do something more valuable?

Or is it missing a log4net reference? Kao, Tian xiaode.

 

Appendix: refer to the getcurrentclasslogger () method of nlog to encapsulate a log class.

The project uses nlog to record logs, which are often referenced in different services (class libraries. DLL, and then you need to record some logs according to the date and Class Name (related to the configuration, here), so the call is usually written like this:

Private Static ReadonlyNlog. Logger logger = nlog. logmanager. getcurrentclasslogger ();

If there are many class libraries in the solution, it is annoying to write the above line of code in this class to reference DLL files and record logs at the same time, so you must think of encapsulating and simplifying the call method.

Check the source code of logmanager. getcurrentclasslogger (). We can see that it is implemented as follows:

 
Logmanager. getcurrentclasslogger// Nlog. logmanagerPublic StaticLogger getcurrentclasslogger () {stackframe =NewStackframe (1,False);ReturnLogmanager. getlogger (stackframe. getmethod (). declaringtype. fullname );}

 

The code is very simple, that is, the stack technology is used to obtain the caller's class name of a method, and the method for recording logs by class name is very similar to the implementation method in log4net. In this way, we can encapsulate a public logger class and use static methods to implement most common logging methods in nlog. Below is a simple example of logging error logs:

LoggerPublic ClassLogger//: Nlog. Logger{Public Static VoidError (StringMessage,Params Object[] Parameters) {var stackframe =NewStackframe (1,False); Var logger = logmanager. getlogger (stackframe. getmethod (). declaringtype. fullname); logger. Error (message, parameters );}}

 

In fact, logger in this implementation can also use its name cache, without the need to write every time:

VaR stackframe =NewStackframe (1,False); Var logger = logmanager. getlogger (stackframe. getmethod (). declaringtype. fullname );

 

After all, reflection is very performance-consuming.

Generally, this logger class is encapsulated in a public class library. Later, you can directly reference the public class library project, and record the log to be called in the form of logger. Error. This is a natural pleasure.

Regarding Exception Handling, according to this article by Niu renchen of the blog, the exception was captured and tracked to a poor position by Microsoft. As one of the people who do not know the truth, I think it would be better to keep a record about how to solve the problem quickly in the future.

Reference: http://msdn.microsoft.com/zh-cn/library/33k1wxtt (V = vs.95). aspx

Http://www.cnblogs.com/zc22/archive/2009/12/25/1631773.html

 

Supplement 1: understanding the differences between components and services

The text below is from Martin Fowler'sArticleInversion of control containers and the dependency injection part of the pattern (components and services). After your reference, you can translate a short section and write your own understanding.

Original article:

Components and services

The topic of wiring elements together drags me almost immediately into the knotty terminology problems that surround the terms service and component. you find long and contradictory articles on the definition of these things with definitions. for my purposes here are my current uses of these overloaded terms.
I use component to mean a glob of software that's intended to be used, without change, by an application that is out of the control of the writers of the component. by 'without change 'I mean that the using application doesn' t change the source code of the components, although they may alter the component's behavior by extending it in ways allowed by the component writers.
A service is similar to a component in that it's used by foreign applications. the main difference is that I have CT a component to be used locally (think JAR file, assembly, DLL, or a source import ). A service will be used remotely through some remote interface, either synchronous or asynchronous (eg web service, messaging system, RPC, or socket .)
I mostly use service in this article, but much of the same logic can be applied to local components too. indeed often you need some kind of local component framework to easily access a remote service. but writing "component or service" is tiring to read and write, and services are much more fashionable at the moment.

 

Similarities and differences between components and services

How to distinguish between "service" and "component )?
The so-called"ComponentsIt refers to a software unit that is beyond the control of the author.ProgramBut the latter cannot modify the component. That is to say, an application using a component cannot modify the component'sSource codeBut it can be extended by the author to change the behavior of components.
Services and components have some similarities: they will be used by external applications.

In my opinion (here I am not referring to Lou pig, Martin Fowler, Lou pig Note), the biggest difference between the two is:Components are locally used (such as jar files, assembly, DLL, or source code import ); services must be remotely used through -- synchronous or asynchronous -- remote interfaces (such as Web Service, message system, RPC, or socket).

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.