.NET Core creates a console program
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Microsoft's Windows 11 2022 Update (22H2) enables CoreIsolation's memory integrity protection by default. However, if you are running an older version of the operating system, such as Windows 11 2022 Update (22H1), you will need to turn this feature on manually. Turn on CoreIsolation's Memory Integrity feature in Windows 11 For users who don't know about Core Isolation, it's a security process designed to protect basic core activities on Windows from malicious programs by isolating them in memory. This process, combined with the memory integrity feature, ensures

Nintendo has opened pre-orders for the latest version of the Switch Lite (curr. $189.99 on Amazon). However, the device is not available to order globally just yet. To recap, the company presented the Switch Lite Hyrule Edition almost two weeks ago d

Core has two meanings in computers: 1. The core, also known as the core, is the most important component of the CPU. All calculations, accepting storage commands, and processing data of the CPU are performed by the core; 2. Core, core is Intel's processor Name, Core is the processor brand launched by Intel after the Pentium processor. It has currently released twelfth generation Core processors.

Console means console. It is a device or software that interacts with a computer system. It is usually a device with a keyboard and a screen for inputting and outputting information. The console was originally Used for large computer systems, and later also applied to personal computers and servers, it can help users manage and maintain computer systems, as well as install operating systems and applications, debug programs, etc.

Use the Console.Clear function in C# to clear the console output. In C# console applications, we often need to clear the output information in the console in order to display new content or provide a better user experience. C# provides the Console.Clear function to implement this function, which can clear the output in the console and make the interface blank again. The calling format of the Console.Clear function is as follows: Console.Clear(); This function does not require any input

Nintendo presented plenty of games yesterday during its most recent Nintendo Direct event, an overview of which we have provided separately. Additionally, the company also announced a new version of the Switch Lite (curr. $194.93 on Amazon), possibly

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

If you purchased the MagicX XU Mini M recently, this news might come as a surprise. A hardware and software teardown of the newly released handheld console revealed that the advertised RK3562 CPU is, in fact, a lower-specced, older RK3326 processor.
