Posts

Showing posts from March, 2022

.Net Core on IIS

Install ASP.NET Core Modul https://docs.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-6.0&tabs=visual-studio   Create a .net core app and publish to folder Create IIS site and point to publish folder https://docs.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-6.0&tabs=visual-studio Specify ASPNETCORE_ENVIRONMENT https://ifyoudo.net/how-to-setup-aspnetcore_environment-variable-on-an-iis-website

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="log4net.Layout.PatternLayout">

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 type = "log4net.Layout.PatternLayout" >             < conversionPattern value = "%d [%t] %-5p %logger - %m%n" />         </ layout >     </ appende

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" />         </ layout >     </ appe

EF Core

Image
EF Core 5 EF Core 5 does not work with .net framework. It works with .net core 3.0 and above Many to Many Relationships EF core can handle a simple many to many relationship without including a class that represents the associative table. All you need to do is add navigation properties between the two classes that have a many to many relationship. for a more complex many to many relationship, a class will need to be created to represent the associate table. In addition, the OnModelCreating must be updated to define the many to many relationship. Use fluent methods: HasMany, WithMany, and UsingEntity Bulk Operation it will not work when working with less than 4 insertion/update example of increasing and decreasing batch size