Nlog class library usage exploration (1) + practice insights

Source: Internet
Author: User
Tags silverlight
1. Why did I learn software?

1. failed to pass the college entrance examination, low score, and no option

2. Rural Children, learn some technologies, and have a mouthful of food in the future

3. have technical skills and want to play with computers (at that time, I thought computer learning was just a computer)

1.2 Why C # is selected #

1. I learned C, C ++, and net and Java. I think C, C ++, and C # looks nice and readers can just choose.

2. Most of them learn Java better and don't want to choose C #

1.3 choosing to determine fate

Winform, Asp.net, and windowsphone each studied for half a year. The best learning method is WP. I can only explain it a little. With the C # Foundation, there is nothing to do with the project, except for some management systems. From then on, I was confused about the net platform. I did not have the opportunity to find a job if I had a chance to study for a graduate student. In fact, I personally think that it is not very useful for postgraduate students. However, when they met a group of good friends, they were very grateful. I played League of legends for one year during my postgraduate studies. I had a year of crazy because I had never played any games before. Then I am looking for an internship, but I also have some experience in net, so I can only find this internship. However, I always think that net is not popular, and the future is worrying. So far, I think so. In fact, there are also Java-related jobs, but I am not confident enough to sign in school.

1.4 thoughts on the practice of Emperor Metropolis

In July 1, 2014, I had no choice but to seek an internship at didu. In fact, I didn't want to come at all, but I wanted to earn some tuition. I have a bachelor's degree and a few good friends who have just been working for a year, so I have not suffered any sin. I was not cheated and did not bother with my residence. I lived in a friend and later went away, but that was my last words. I posted 20 resumes while I was away from school. I thought that if I hadn't found the 20 copies, I would just do business or sales. My resume is based on WP and Silverlight experience. I am directly investing in a WP position. I feel that it doesn't matter what I do. I just need to pay too much for my salary. Android and IOS have also been invested. Although I have no experience or foundation, I feel that it is not difficult to learn. He is mainly engaged in the WP position and the C # engineer position. On average, there are two phone numbers for interviews every day. Most of them are small and medium-sized companies, and there are even less than 10 companies. However, if I had to make a call, I had to go to the interview and did not want to go any time. I thought it was a respect. I won't talk about the interview process. The smaller the company, the more I feel that I want to die and feel that you have no experience. Big companies are very respectful to people and give people a warm feeling. They're all programmers. They're so anxious !!

While I was looking for a job, I was always immersed in crowded subway and bus, and I felt that the emperor did not feel excited at all. This is not happiness. Far away. With a little WP experience and enthusiasm for programming, sometimes enthusiasm can really be used as a meal. I interviewed eight employees in a week and offered three employees in total. Both of them are under five thousand, WP position. The reason for the low salary tells me that because you do not have a diploma, you cannot pay according to your ideal salary. I actually want to do mobile development. However, the salary is too cold in Beijing. The third offer is a C # engineer, with a high salary and a better office environment. But the disadvantage is that there is no one to bring it. It is just a C # engineer. The main responsibilities are WPF, windowsphone, and windows8 ..... To put it bluntly, as long as it is related to net, it is yours. In order to relieve the burden on my parents, I chose a better salary, although I felt a little bad for my own development. I don't know if the selection is correct. What do you think? This study is estimated to be net in the future, and it will be difficult to change the direction of graduation. No one answered the question, solved the problem by myself, no one discussed it, no one spoke. It's such a day, ah, quiet !!! In any case, it is the first job. After all, it is the "First Love", and I will do my best, for fear that the company will not be able to pay the salary. After learning WPF for a month, I think it is difficult for me to say that it is an interface (haha, the company didn't have a blend designer, but there is no way to do it. I have no art cells myself ). Now there are no project tasks. Simply study the source code of the company's existing mature projects and review the WPF knowledge. If you have never used it during the learning process, study it carefully. I want to see if it is faster. I hope you can give me any suggestions. Sometimes it's always lingering. How long can this path be programmed? Do you want to continue this path after graduation? This may be hard to answer.

The following is a question about writing logs when I look at the company's source code today. The project uses an nlog class library, which has never been used before. Skip this step if you are not interested.

2. Go to question 2.1 and define

Official definition:Nlog is a free Logging Platform. net, Silverlight and Windows Phone with rich log routing and management capabilities. it makes it easy to produce and manage high-quality logs for your application regardless of its size or complexity. simply put, nlog is an open-source log management platform. net, Silverlight, and Windows Phone. It has rich routing and management functions. You can easily generate and manage your project logs.

Nlog is an open source project hosted on GitHub and source code is available under the terms of BSD license. // you can go to the source code on GitHub. The source code is based on BSD license.

Binary packages are available via nuget. // you can use nuget to directly import the class library to the project.

For the new student. What's hard to do is

2.2 nlog features

In short, nlog can export all the diagnostic information based on the. NET language (C #, VB. The data context (contextual information, including the occurrence time, thread, process, and severity) is used to format the information you want to export. Where is the output? Nlog provides export targets (targets text, database, email, console, etc)

2.3 practical Configuration

Very easy to configure, both through configuration file and programmatically. This sentence is the most important in the official document. It shows how to apply it to your project. Two methods are provided: configuration files and background programming.

  • Files-Single file or multiple, with automatic file naming and archival
  • Event Log-Local or remote
  • Database-Store your logs in databases supported by. net
  • Network-Using TCP, UDP, soap, MSMQ protocols
  • Command-line Console-Including color coding of messages
  • Email-You can receive emails whenever application errors occur

The above links provide examples of log output to files, remote logs, databases, mailboxes, etc. The following describes how to use a project instance.

3. A small example of project instance 3.1 output destination as the Console

Step 1: Use netget to import the class library DLL.

Step 2:Open the configuration file nlog. config

Step 3:Define the export target and output information

Add a target and specify the output layout. That is to say, where do we want to output project logs and what is the output format?

<Target xsi: TYPE = "console" name = "conso"
Layout = "$ {longdate }$ {uppercase: $ {level }}$ {message}"/>

Layout is a fixed format and $ {} is a syntax format. The output information is time, level, and information respectively.

Step 4:Define output rules

What information should we output to the log file? This involves the record level issue.

Log Level Information, used to describe the importance of this information. Nlog supports the following record levels:

  1. Trace-The most common record information, generally used for normal output
  2. Debug-The same information is recorded, but the frequency is less than that of Trace, which is generally used to debug programs.
  3. Info-Message of information type
  4. Warn-Warning information, which is generally used in important scenarios
  5. Error-Error message
  6. Fatal-Fatal exception information. Generally, the program cannot be executed after a fatal exception occurs.

In this example, we set the output level to a level equal to or higherDebug levelTo the console.

<Logger name = "*" minlevel = "debug" writeto = "conso"> </logger> this statement is clear, but it must be noted that the write to attribute should be the target name.. It indicates writing to the output target.

Step 5:Create a logger object. If you want to generate and output diagnostic information, you must add a logger object. The method name of the logger object is the same as that of the record level (debug (), Info (), fatal ()......). The logger object is created through the logmanager object. It is recommended that the logger object name be the same as the program class name. Call the getcurrentclasslogger () method of logmanager to automatically create a logger object for the current class. The background code is as follows:

Using nlog; namespace wpfapplication4 {/// <summary> /// mainwindow. interaction logic of XAML // </Summary> Public partial class mainwindow: window {public static logger mainwindowlogger = logmanager. getcurrentclasslogger (); // This logger object represents the source of the log message associated with the current class. Public mainwindow () {initializecomponent (); mainwindowlogger. debug ("simulate a debug-level information"); // issue a debug-level diagnostic information. }}}

After completing the preceding steps, you should have a basic understanding of the nlog workflow. In the configuration file, configure the output target, which diagnostic information should be output (layout), and define the output rule and the level of information output. Use the logger object in the background code of the application to define the output diagnostic information.

3.2 Further expansion example

We add a new requirement. What if we output data to a file at the same time? What should I do? This is not difficult. You only need to modify the configuration file. Add a target.

The two images have already explained the problem and are not described in detail.

4. Summary

A simple example is just for entry-level people like me. The next article will continue to explore nlog in depth.

Nlog class library usage exploration (1) + practice insights

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.