Posts

Showing posts with the label dotnet

HTML to PDF

Add this nuget package HtmlRenderer.PdfSharp Generate a PDF document string htmlString = "<h1>Document</h1> <p>This is an HTML document which is converted to a pdf file.</p>"; PdfDocument pdfDocument = PdfGenerator.GeneratePdf(htmlString, PageSize.A4); To save to disk pdfDocument.Save("C:/temp/HTML to PDF Document.pdf"); To convert to byte array using (MemoryStream ms = new MemoryStream()) { pdfDocument.Save(ms); byte[] byteArray = ms.ToArray(); .... }

MiniProfiler in ASP.NET Core

 To install dotnet add package MiniProfiler.AspNetCore.Mvc dotnet add package MiniProfiler.EntityFrameworkCore To configure public void ConfigureServices(IServiceCollection services) { .... services.AddMiniProfiler(options => options.RouteBasePath = "/profiler").AddEntityFramework(); .... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseMiniProfiler(); /* Code removed for brevity. */ } To access a list of all requests, navigate to /profiler/results-index To profile methods in a class libray, add this nuget package MiniProfiler.AspNetCore Wrap the piece of code like this using (MiniProfiler.Current.Step("InsertUpdateBillAsync")) { ..... } To wrap a single line await MiniProfiler.Current.Inline(async () => await ProcessCommitteeAsync(bill.committee), "ProcessCommitteeAsync");

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

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