Showing posts with label .Net Core. Show all posts
Showing posts with label .Net Core. Show all posts

Friday, June 22, 2018

Dotnet Core console app with NLOG in Visual Studio Code

In this post I am going to create simple minimalistic dotnet core console application and integrate it with nlog.



Let's create a folder for our project. I am going to name it nlog_console.



$mkdir nlog_console
$cd nlog_console



Now we create dotnet core console app project and if you have Visual Studio Code installed run the second command below.



$dotnet new console
$code . &



Now lets add nlog package to a project.



$dotnet add package nlog



This will add the following code to our project file.



<ItemGroup>
<PackageReference Include="nlog" Version="4.5.6" />
</ItemGroup>



Nlog framework requires configuration file 'nlog.config' to specify logger tagerts and rules. We can copy paste the sample nlog.config file from this github page or use one below.



xml version="1.0" encoding="utf-8" ?>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="console-example-internal.log"
internalLogLevel="Info" >



<targets>

<target xsi:type="File" name="file"
fileName="console-example.log"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
<target xsi:type="Console" name="console"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
</targets>


<rules>
<logger name="*" minlevel="Trace" writeTo="file,console" />

</rules>
</nlog>



'nlog.config' file need to be copied to destination directory during project build. To achieve it add the code below to a project file.



<ItemGroup>
<None Include="nlog.config" CopyToOutputDirectory="Always" />
<ItemGroup>



Now we need to add actual logging code to a program. Copy paste the Program class code below.



using System;

namespace dotnetcore_nlog
{
class Program
{
private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
log.Trace("Loggin trace.");
}
}
}



To build and run our console app run code below or add launch.json config to Visual Studio Code.



$dotnet build
$dotnet run



You should see the following output and log file created in the same directory as our console app.



2018/06/19 18:48:13.130|TRACE|Loggin trace. |nlog_console.Program|


References:





0

Monday, December 5, 2016

Reading appsettings.json in .Net Core Console Application under Linux

Recently I was looking at how to load and use appsettings.json file in .Net Core Console application. Many resources on .Net forums and sites were descripting use of appsettings.json file in ASP.NET Core web App, but very little described appsettings with .Net Core Console app. I managed to created sample .Net Core app with the minimum required libraries. Creation of this sample program I am going to describe here. Let's call our App "appsettings".

$mkdir appsettings
$cd appsettings
$dotnet new
$dotnet restore

Now create appsettings.json file:

{
"AppSettings": {
"Date": "1900-01-01"
}
}

Date is the setting I am interested in loading from AppSettings section.



Edit project.json file to include "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" in dependencies section. This is the only extra library we need in order to use ConfigurationBuilder functions.

{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"outputName": "AppSettings"
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
},
"Microsoft.Extensions.Configuration.UserSecrets": "1.1.0"
},
"imports": "dnxcore50"
}
}
}

Now lets edit the Program.cs file. We are going to load the Date setting and display it in Console. Don't forget to include "using Microsoft.Extensions.Configuration;"

using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace ConsoleApplication
{
public class Program
{
public static string AppSettingsFile = "appsettings.json";
public static string AppSettingsSection = "AppSettings";
public static string DateSetting = "Date";

public static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(Program.AppSettingsFile, optional: true,
reloadOnChange: true);

var
config = builder.Build();
var appSettings = config.GetSection(Program.AppSettingsSection);
string dateString = appSettings[Program.DateSetting];
Console.WriteLine("Date:" + dateString);
}
}
}

That is pretty much all. Now let's build and run it.

$dotnet build
$dotnet run
Date:1900-01-01

0

Tuesday, July 5, 2016

.Net Core 1.0 Released! Or installing .Net Core on Debian

This was a great news to me that .Net Core 1.0 was released. I already was trying .Net Core RC2 on my Debian 8.x so I prepared to go through the installation process of new release. Surprisingly installation of .Net Core 1.0 described in Install for Debian 8 on Microsoft site was very smooth in comparison to beta versions. All I had to do is to clean up my previous install from ~/.dotnet go through the instructions on install page and "Hello World!" programs runs. Very pleased to see that improvement.



So here I am going to provide some links to what to do next once you get your install done too.




References:













0