Home  >  Article  >  Backend Development  >  .NET Core creates a console program

.NET Core creates a console program

Y2J
Y2JOriginal
2018-05-23 15:07:556335browse

This article mainly introduces in detail how .NET Core creates a console program, which has certain reference value. Interested friends can refer to

.NET Core version: 1.0. 0-rc2

Visual Studio version: Microsoft Visual Studio Community 2015 Update 2

Development and running platform: Windows 7 Professional Edition Service Pack 1

Added one Console project(ConsoleLogApp)

Add 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"
  }
 }
}

Added new log output configuration file (log.json)

The main purpose of our console program is to print out logs, so a separate log configuration file is used here to save relevant log related options. For example: 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 include the 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)
    {
      // 支持中文编码
      Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

      // 加载日志配置文件
      var setttins = new ConfigurationBuilder().AddJsonFile("log.json").Build();

      // 创建ConsoleLogProvider并根据日志类目名称(CategoryName)生成Logger实例
      var logger = new ServiceCollection().AddLogging().BuildServiceProvider().GetService<ILoggerFactory>().AddConsole(setttins).CreateLogger("App");

      // 设置事件ID
      const int eventId = 888888;

      // 输出正常提示日志
      logger.LogInformation(eventId, "订单号({OderNo})", "12345678000");

      // 输出警示日志
      logger.LogWarning(eventId, "待处理订单数达到5分钟内的预警值:{max}", 2000);

      // 输出错误日志
      logger.LogError(eventId, "数据库连接超时");

      Console.ReadLine();
    }
  }
}

Use "dotnet restore" to restore dependencies

Switch the current directory in Git Bash Go 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, and then use the NuGet library to restore the dependencies of the entire project library, then traverse each directory to generate a project file, and continue to restore dependencies in the project file.

Use "dotnet build" to compile the entire project

After the compilation is successful, we The compiled folder (D:\ConsoleLogApp\bin\Debug\netcoreapp1.0) is found in the bin directory under the project root directory. After the command is executed successfully, a Debug directory is generated and a file with the application name is generated in this directory. Named folder (netcoreapp1.0, this name is configured in project.json)

Use "dotnet run" to run the program

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

The above is the detailed content of .NET Core creates a console program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn