. net core creates a console program


. net core version: 1.0.0-rc2

Visual studio version: Microsoft Visual Studio community 2015 Update 2

Development and operation platform: Windows 7 professional service pack 1

Add a console itemConsoleLogApp)

stayNew dependency in 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 profile(log.json)

Our console program is mainly used to print and output logs, so a separate log configuration file is used to save relevant log related options, such as whether to include context, the lowest level of log output, etc.

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

Includescopes is false, so that the console log output does not contain context; The lowest level of loglevel is set to warning. Only logs higher than this level will be output. App is the CategoryName of the log.

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)
      //Support Chinese coding

      //Load log profile
      var setttins = new ConfigurationBuilder().AddJsonFile("log.json").Build();

      //Create a consolelogprovider and generate a logger instance based on the log category name (CategoryName)
      var logger = new ServiceCollection().AddLogging().BuildServiceProvider().GetService<ILoggerFactory>().AddConsole(setttins).CreateLogger("App");

      //Set event ID
      const int eventId = 888888;

      //Output normal prompt log
      Logger.loginformation (eventid, "Order No. ({oderno})", "12345678000");

      //Output alert log
      Logger.logwarning (eventid, "warning value of orders to be processed within 5 minutes: {Max}", 2000);

      //Output error log
      Logger.logerror (eventid, "database connection timeout");


Use “dotnet restore” to restore dependencies

In Git bash, switch the current directory to the root directory of the project (in this example, D: \ consolelogapp)

The dotnet restore command is used to find the project file (project. JSON) in the current directory, then use the nuget library to restore the dependency Library of the whole project, then traverse each directory to generate the 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 project root directory. After the command is executed successfully, a debug directory is generated 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 logs of info level are filtered out, and only those above warning are output.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.