. NET Core creates a Console program, coreconsole

Source: Internet
Author: User

. NET Core creates a Console program, coreconsole

. NET Core version: 1.0.0-rc2

Visual Studio version: Microsoft Visual Studio Community 2015 Update 2

Development and running platform: Windows 7 Professional Service Pack 1

  • Add a console Project (ConsoleLogApp)

  • Add a dependency 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"    }  }}
  • Add log output configuration file (log. json)

The main purpose of this console program is to print output logs. Therefore, a separate log configuration file is used to save related log options, such as whether to include the context and the lowest log output level.

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

When IncludeScopes is set to false, the console log output does not contain the context. The lowest level of LogLevel is set to Warning. Only logs above this level are output. The App is the log CategoryName.

  • Entry 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) {// supports Chinese Encoding. registerProvider (CodePagesEncodingProvider. instance); // load the log configuration file var setttins = new ConfigurationBuilder (). addJsonFile ("log. json "). build (); // create a ConsoleLogProvider and generate a Logger instance var logger = new ServiceCollection () based on the log category name (CategoryName (). addLogging (). buildServiceProvider (). getService <ILoggerFactory> (). addConsole (setttins ). createLogger ("App"); // set the event ID const int eventId = 888888; // the log is prompted normally. logInformation (eventId, "Order Number ({OderNo})", "12345678000"); // output the warning log logger. logWarning (eventId, "Warning value for the number of pending orders reaching 5 minutes: {max}", 2000); // output the error log logger. logError (eventId, "database connection timeout"); Console. readLine ();}}}
  • Use "dotnet restore" to restore Dependencies
    • Switch the current directory to the project root directory in Git Bash (in this example, D: \ ConsoleLogApp)
    • The dotnet restore command is used to find the project file (project. json). Then, use the NuGet library to restore the dependent libraries of the entire project, traverse each directory to generate a project file, and continue to restore the dependencies in the project file.

  • Use "dotnet build" to compile the entire project

After the compilation is successful, we find the compiled folder (D: \ ConsoleLogApp \ bin \ Debug \ netcoreapp1.0) in the bin directory under the root directory of the project ), after the command is successfully executed, a Debug directory is generated and a folder named after the application (netcoreapp1.0, the name is in the project. json)

  • Run the program using "dotnet run"

We can see that info-level output logs are filtered out, and only logs above Warning are output.

 

 

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.