. 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.
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.