Posts

Showing posts with the label C#

Log4net in .net 6 webapi

Create a new web api project dotnet new webapi Add log4net dotnet add package Microsoft.Extensions.Logging.Log4Net.AspNetCore Add builder.Logging.AddLog4Net(); call in your Program.cs file. ... var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Logging.AddLog4Net(); //here var app = builder.Build(); ... The logging level by default is set to INFO. It can be found in appsettings.json and appsettings.{ENVIRONMENT}.json Create a new file called  log4net.config  and this to the content <?xml version="1.0" encoding="utf-8" ?> <log4net>     <root>         <appender-ref ref="ConsoleAppender"/>     </root>     <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">         <layout type="log4n...

log4net: using StringMatchFilter to filter out unwanted messages

 You can use StringMatchFilter in log4net to discard unwanted messages. An example: < log4net >     < root >         < appender-ref ref = "ConsoleAppender" />     </ root >     < appender name = "ConsoleAppender" type = "log4net.Appender.ConsoleAppender" >         < filter type = "log4net.Filter.StringMatchFilter" >             < stringToMatch value = "message 1" />             < acceptOnMatch value = "false" />         </ filter >         < filter type = "log4net.Filter.StringMatchFilter" >             < stringToMatch value = "message 2" />             < acceptOnMatch value = "false" />         </ filter >         < layout typ...

Log4Net in .NetCore simple example

This is a simple example of using Log4net in .net core console app. Create a new console app dotnet new console Add log4net dotnet add package log4net Replace Progam.cs with this using System.Reflection; using log4net; using log4net.Config; var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); var logrepo = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.Configure(logrepo, new FileInfo("log4net.config")); log.Info("this is INFO"); Create a new file log4net.config and add this < log4net >     < root >         < appender-ref ref = "ConsoleAppender" />     </ root >     < appender name = "ConsoleAppender" type = "log4net.Appender.ConsoleAppender" >         < layout type = "log4net.Layout.PatternLayout" >             < conversionPattern value = "%d [%t] %-5p %logger - %m%n" />   ...

dotnet migration from command line

Image
dotnet from command line can do more than just create projects and add packages. It can be used to work with ef core migrations. Lets start by creating a new console app. Run this command:  dotnet new console . Add these three packages: dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore.SqlServer Your csproj should now look like this: <Project Sdk= "Microsoft.NET.Sdk" > <PropertyGroup> <OutputType> Exe </OutputType> <TargetFramework> netcoreapp2.2 </TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include= "Microsoft.EntityFrameworkCore" Version= "2.2.6" /> <PackageReference Include= "Microsoft.EntityFrameworkCore.Design" Version= "2.2.6" /> <PackageReference Include= "Microsoft.EntityFrameworkCore.S...

Using @CurrentIteration with TFS API

Image
@CurrentIteration is neat macro I use when I created saved queries in TFS. For example, I recently needed to write a query that fetched all current bugs in the sprint: This works great in the TFS web interface. The main reason for me to create this saved query was however was to be able to access it via the TFS API. So I started like this: using ( var workItemTrackingHttpClient = new WorkItemTrackingHttpClient ( new Uri ( " http://mytfs/tfs/Plato " ) , new VssCredentials ( ) ) ) { try { var items = workItemTrackingHttpClient . QueryByIdAsync ( new Guid ( " 25c6e737-d0ce-416f-9f7a-3cb78959334d " ) ) . Result . WorkItems ; } catch ( Exception ex ) { } } This returned the following error: System.AggregateException: One or more errors occurred. ---> Microsoft.VisualStudio.Services.Common.VssServiceException: VS402612: The macro '@CurrentIteration...