. NET core creates a console program

Source: Internet
Author: User
Tags dotnet
This article is a detailed introduction to the. NET core how to create a console program, with a certain reference value, interested in small partners can refer to

. NET Core Version: 1.0.0-RC2

Visual Studio version: Microsoft Visual Studio Community Update 2

Development and Operation Platform: Windows 7 Professional Service Pack 1

Add a console project (Consolelogapp)

new dependencies in the Project.json file

{"Version": "1.0.0-*", "buildoptions": {  "Emitentrypoint": true}, "dependencies": {  "Microsoft.NETCore.App": {   "type": "Platform",   "version": "1.0.0-rc2-3002702"  },  " Microsoft.Extensions.DependencyInjection ":" 1.0.0-rc2-final ",  " Microsoft.Extensions.Logging ":" 1.0.0- Rc2-final ",  " Microsoft.Extensions.Logging.Console ":" 1.0.0-rc2-final ",  " Microsoft.Extensions.Configuration.Json ":" 1.0.0-rc2-final ",  " System.Text.Encoding ":" 4.0.11-rc2-24027 ",  "System.Text.Encoding.CodePages": "4.0.1-rc2-24027"}, "Frameworks": {  "netcoreapp1.0": {   "Imports" : "Dnxcore50"}}  }

new log Output configuration file (Log.json)

The main purpose of our console program is to print out the output log, so there is a separate log configuration file to save relevant log related options, such as: whether to include the context, the lowest level of log output, etc.

{"Includescopes": false, "LogLevel": {  "App": "Warning"}}

Includescopes the console log output is not included in the context, and the lowest level of loglevel is set to: Warning, only logs above this level will be output. The app is the CategoryName of the log.

Portal Program

using system;using microsoft.extensions.configuration;using System.Text;using Microsoft.extensions.dependencyinjection;using microsoft.extensions.logging;namespace ConsoleLogApp{public class program {public static void Main (string[] args) {//Support Chinese encoding encoding.registerprovider (CODEPAGESENCODINGP Rovider.      Instance); Load log config file var setttins = new Configurationbuilder (). Addjsonfile ("Log.json").      Build (); Create the Consolelogprovider and generate the logger instance var logger = new Servicecollection () based on the log class name (CategoryName). Addlogging (). Buildserviceprovider (). Getservice<iloggerfactory> (). Addconsole (Setttins).      Createlogger ("App");      Set Event ID const int EVENTID = 888888; Output normal prompt log logger.      Loginformation (EventId, "order number ({Oderno})", "12345678000"); Output warning Log logger.      Logwarning (EventId, "the number of pending orders reaches the alert value within 5 minutes: {max}", 2000); Output error log logger.      LogError (eventId, "database connection timeout");    Console.ReadLine (); }  }}

To restore a dependency by using "dotnet Restore"

In git bash, switch the current directory to the root of the project (in this case: D:\CONSOLELOGAPP)

The dotnet Restore command is used to locate the project file (Project.json) under the current directory, then use the NuGet Library to restore the entire project's dependent library, then traverse each directory to build the project file and continue to restore the dependencies in the project file.

Use "dotnet build" to compile the entire project

After compiling successfully, we found the compiled folder in the bin directory under the project root directory (D:\ConsoleLogApp\bin\Debug\ netcoreapp1.0), a debug directory is generated after the command executes successfully and a folder named after the application name is generated in this directory (netcoreapp1.0, which is configured in Project.json)

Use "dotnet Run" to run the program

We can see that the output log of the info level is filtered out, and only the logs above warning are output.

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.