Add free search for your website. Sign up now! https://webveta.alightservices.com/
Categories
.Net C#

Some useful C# code snippets:

I have shown some code and discussed some general best practices earlier this morning in a live Youtube video – Some C# reusable code snippets (https://www.youtube.com/watch?v=9SyZjDukvhE) in ALight Technology And Service’s official Youtube channel (https://www.youtube.com/@alighttechnologyandservicesltd). As promised in the video, this blog post has the code snippets.

Some C# reusable code snippets

Concept – 1:

Centralizing config generation into a re-usable library, having a wrapper class around reading the config. Now the consuming classes do not need to know the details of where or how to get config values. For example, the config can be stored in plain-text / encrypted, can be stored in text files or something like AWS Secrets Manager.

public static void GenerateConfig()
{
    var retVal = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(<ConfigFile>, optional: false, reloadOnChange: true)
                .AddJsonFile(<ConfigFile2>, optional: false, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

    ConfigHelper.Configuration = retVal;
}

Code for converting DateTime into Unix epoch and back to DateTime

private static DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0);

public static long GetUNIXDate(DateTime value)
{
    return (Int64)value.Subtract(epoch).TotalSeconds;
}

public static DateTime GetNormalDateFromUnix(long value)
{
    return epoch.AddSeconds(value);
}

Code for determining if a string consists entirely of ASCII text or not

public static bool IsASCII(this string value)
{
    return Encoding.UTF8.GetByteCount(value) == value.Length;
}

Code for removing non-ASCII characters and retrieving just the ASCII characters from a string

private static readonly Regex asciiRegex = new Regex(@"[^\u0000-\u007F]+", RegexOptions.Compiled);

public static string GetASCIIOnly(string value)
{
    if (value == null) return String.Empty;
    return asciiRegex.Replace(value, String.Empty);
}

Code for getting a smaller chunk of a long string and if necessary append … at the end – useful on web pages showing first few characters of a longer text.

public static string GetSnippet(string value, int length, bool appendDots)
{
    if (String.IsNullOrWhiteSpace(value)) return String.Empty;

    if (value.Length < length - 3) return value;

    if (appendDots)
    {
        return $"{value.Substring(0, length - 3)}...";
    }
    else
    {
        return value.Substring(0, length);
    }
}

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net Architecture C#

Some C# reusable code snippets – Video

Some C# reusable code snippets – Video

Categories
Welcome

Introduction to collectd for metrics

As I have mentioned in a previous blog post – Business activities would continue normal, the primary focus right now is enhancing monitoring and alerting. The strategy is to have a centralized logging and monitoring. Then anomaly detection and alerts. As part of this effort, I came across an excellent utility known as collectd. This tool is easy to install and configure. An alternate is statsd.

I have been looking for metric tools i.e servers that are lightweight, easy to configure and can be easily used from C# applications i.e I want to ingest metrics from my C# applications and consume from C# applications. collectd has GRPC plugin, this would mean a GRPC application can be developed in any programming language. I would definitely provide some code examples for ingesting metrics and reading metrics in the future. For now this is a getting started with collectd blog post.

The installation instructions are bit messed up for collectd, specifically the service file of collectd needs to be manually edited based upon your installation location. As of now, I am collecting some simpler metrics, but there are some plugins that would allow for enhanced metrics gathering.

All the logs are being ingested into AWS CloudWatch. Now, I would be developing some dashboards for monitoring, setting up alerting rules etc… I don’t like existing monitoring front-ends due to very less security restrictions. I want my dashboard to be very secure.

Based on collectd – wiki – first steps here is a summary of installation procedure:

The list of library dependencies for plugins can be found here: README

> sudo apt-get install build-essential
// Install any necessary dependency libraries of plugins, I have installed the following
> sudo apt-get install libgrpc-dev libiptc-dev libmysqlclient-dev libprotobuf-dev libprotobuf-c-dev
> cd cd /tmp/
> wget https://storage.googleapis.com/collectd-tarballs/collectd-5.12.0.tar.bz2
> tar jxf collectd-5.12.0.tar.bz2
> cd collectd-5.12.0
> ./configure
> make all install
> sudo nano /opt/collectd/etc/collectd.conf

I have enabled logfile, syslog, cpu, csv, df, disk, load, memory, swap, uptime, users plugins. These plugins require very less configuration. For csv specify an directory in configuration, for logfile specify a file.

The following was for Ubuntu, but based upon your system edit the collectd.service file. Look for the proper location of the config file, binary and then enable and start the service. On Ubuntu:

Service file: /lib/systemd/system/collectd.service

Configuration file: /opt/collectd/etc/collectd.conf

Binary: /opt/collectd/sbin/collectd

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net C#

File tailer in C#

As mentioned in a previous post – Cancelling all activities at ALight Technology And Services Limited. I am not seriously working on anything within ALight Technology And Services Limited, but I am still .Net developer. I have some internal logging and monitoring implemented. I have used Cloudwatch Agent and Promtail for ingesting logs into AWS Cloudwatch and Grafana. Both are great platforms but I wanted to implement a similar tool in C#. Here is some sample code, explanation capable of doing something similar.

First store the name of file, Creation Date, Size, Last Modified Date. These can be accessed from FileInfo object.

var fi = new FileInfo("path");
fi.CreationTimeUtc	
fi.LastWriteTimeUtc	
fi.Length

Now, in a loop read the file, if new file i.e CreationTime is different read from beginning. If Length or LastWriteTime are different but same CreationTime, do a seek.

long seek = 0;

while(<some condition>)
{
   using(sr = new StreamReader(fi.OpenRead()){
      sr.BaseStream.Seek(seek, SeekOrigin.Begin);
      var data = await sr.ReadToEndAsync();
      seek += data.Length;
      // Do something with data
      // Store filename, seek in some persistent storage like a file
}

The above code block shows some sample code for using seek, we would store the variable seek along with FileCreation, Update, Length. Even if the application is restarted, the application would work properly.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
Security Wordpress

How I secured my wordpress account!

Cross post – https://kantikalyan.medium.com/how-i-secured-my-wordpress-account-d162f1c0934c

On December 22nd at 17:45 India Standard Time (12:15 GMT / 07:15 EST), I am doing a live video on showing the security. That’s why they were not able to hack my WordPress although they had a very powerful spying / hacking equipment.

YubiKey Bio:

I have Yubikey Bio, it’s a biometric authentication USB device. Some websites support multi-factor authentication with hardware devices such as Yubikey. The difference between normal hardware keys and Yubikey Bio is the biometric authentication. With normal hardware keys anyone with access to the USB device can login, but with Yubikey Bio – biometric authentication happens i.e Yubikey Bio verifies fingerprint.

Nextend Social Login Plugin for WordPress:

Nextend Social Login Plugin – This plugin allows me to login via Google. There is a little setup in GCP console. But ultimately allows me to use Google login. I have configured in such a way that only admin@alightservices.com is allowed to login using Google authentication. I have secured my Google login to use Yubikey Bio.

Duo Two-Factor Authentication:

Duo Two-Factor Authentication allows further securing the wordpress installation by using Yubikey Bio. There is a little bit of configuration to be done.

In this setup I first need to login into my Google account – admin@alightservices.com, then I am prompted for Biometric authentication. Then I login into wordpress and once again I am prompted for biometric authentication. This way no one else can login into my WordPress account.

By reviewing the logs, there have been several thousand login attempts but all of those have been thwarted with this setup. i.e even with proper password, they can get to the MFA screen but not any further.

India’s R&AW spies have a very powerful spying / hacking equipment. I think it might be invisible drone with very powerful capabilities such as recording video, audio, speakers used for whispering and even mind reading capabilities. With such a powerful hacking equipment, normal usernames and passwords are obsolete. The list of hackers/impersonators/identity thieves might include: erra surnamed people – diwakar / karan / kamalakar / karunkar / erra sowmya / erra sowjanya / zinnabathuni sowjanya / thota veera / uttam / bojja srinivas / mukesh golla / bandhavi / female identity thieves who claim to have my first name – Kanti and their helper pimp Kalyan’s (I am Kanti Kalyan Arumilli – those escorts and pimps tried to break my identity). Some of them have multiple aliases and multiple surnamed virtual identities.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net C# DailyReads

DailyReads 20/12/2022

How to Secure Passwords with BCrypt.NET

“In this article, we are going to learn how to secure passwords with BCrypt.NET to ensure we are up to the industry standards when it comes to security in our .NET environment.”

Schedule Jobs with Quartz.NET

“Recurring, background tasks are widespread and very common when building applications. These tasks can be long-running or repetitive and we don’t want to run them in the foreground, as they could affect the user’s experience of the application. So instead we must schedule these jobs to run in the background somewhere. To achieve this in .NET, we can use the Quartz.NET library to manage the creation and scheduling of these jobs.”

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net C# DailyReads

Daily Reads 19/12/2022

Nullable Types in C#

In this article, we will extensively break down the concept of Nullable types in C#. We will discuss the kinds of Nullable types in C#, their characteristics, and how we can use them when building applications...”

How To Structure Your .NET Solutions: Architecture And Trade-Offs

“How should you design the structure of your .NET solutions? Microservices? Monolith? Feature folders? Clean architecture? Shared databases?…”

Frozen collections in .NET 8

“.NET 7 was freshly released but Microsoft does not sleep. .NET 8 is already in the making and I want to showcase to you one new area where the dotnet team is working on Frozen collections…”

PriorityQueues on .NET 7 and C# 11

“Starting from .NET 6 and C# 10, we finally have built-in support for PriorityQueues…”

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net Architecture C# gRPC

Little bit of live coding for PodDB January 1st 2023 update

PodDB has been decommissioned in November 2023!

https://www.youtube.com/watch?v=o5lhNu6vwbk
Categories
.Net

Nuget Repository for ALight Technology And Services Limited

The Founder & CEO of ALight Technology And Services Limited, Mr. Kanti Kalyan Arumilli is committed to share I.T knowledge, code snippets and some open source development.

A new nuget account has been created for publishing nuget packages. As of now there are no published packages yet. Sitemap Generator package can be expected soon. I do have some plans of NLog logger packages but cannot confirm yet. The URL of the nuget package repository is: https://www.nuget.org/profiles/ALightTechnologyAndServicesLimited.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net ASP.Net C# DailyReads

Daily Reads 17/12/2022

Build a web API with minimal API, ASP.NET Core, and .NET 6

20 minutes module on Microsoft Learn

Use a database with minimal API, Entity Framework Core, and ASP.NET Core

35 minutes module on Microsoft Learn

Create a full stack application by using React and minimal API for ASP.NET Core

28 minutes module on Microsoft Learn

The above 3 are part of Create web apps and services with ASP.NET Core, minimal API, and .NET 6 learning path.

Canceling abandoned requests in ASP.NET Core – Blog article on how to stop processing of abandoned requests in ASP.Net MVC i.e if a browser loads a page but clicks stop or presses escape key. This way, some server-side resources can be saved.

Two of my favorite and absolutely free desktop based (downloadable) software for diagrams – cloud architecture, UML, Database ER etc… Very useful for small startups and developers.

  1. Diagrams.net
  2. Dia

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.