DgcMe.Logging
1.0.2
dotnet add package DgcMe.Logging --version 1.0.2
NuGet\Install-Package DgcMe.Logging -Version 1.0.2
<PackageReference Include="DgcMe.Logging" Version="1.0.2" />
<PackageVersion Include="DgcMe.Logging" Version="1.0.2" />
<PackageReference Include="DgcMe.Logging" />
paket add DgcMe.Logging --version 1.0.2
#r "nuget: DgcMe.Logging, 1.0.2"
#:package DgcMe.Logging@1.0.2
#addin nuget:?package=DgcMe.Logging&version=1.0.2
#tool nuget:?package=DgcMe.Logging&version=1.0.2
DgcMe Library
This library provides various classes and methods that will help you save time and avoid repetitive tasks among different projects. This is a work in progress and will be updated frequently.
Table of Contents
- Installation
- License
- DgcMe
- DgcMe.Logging .NET 6
- DgcMe.Result .NET 6
- DgcMe.System .NET 6 (Windows only)
- DgcMe.Test
Installation
You can install the package via Nuget:
Install-Package DgcMe
Install-Package DgcMe.Logging
Install-Package DgcMe.Result
Install-Package DgcMe.System
License
This project is licensed under the MIT License - see the LICENSE.md file for details
DgcMe
Extensions
String Extensions
var str = "Hello World";
var removedText = str.RemoveText("World"); // Hello
var before = str.GetBefore("World"); // Hello
var after = str.GetAfter("Wo"); // rld
var between = str.GetBetween("Hell", "orld"); // o W
var isNullOrEmpty = str.IsNullOrEmpty(); // false
var isNullOrWhiteSpace = str.IsNullOrWhiteSpace(); // false
var isNotNullOrEmpty = str.IsNotNullOrEmpty(); // true
var isNotNullOrWhiteSpace = str.IsNotNullOrWhiteSpace(); // true
var bytes = str.GetBytes(); // byte[]
var number = "1231";
var converted = number.ConvertTo<int>(); // 1231
var convertedNullable = number.ConvertTo<int?>(); // 1231
var formatText = "Hello {0}".Format("World"); // Hello World
var whiteSpaceRemoved = "Hello World".RemoveWhiteSpace(); // HelloWorld
var boolean = "true".ToBoolean(); // true
var boolean2 = "false".ToBoolean(); // false
var boolean3 = "0".ToBoolean(); // false
var boolean4 = "1".ToBoolean(); // true
var boolean5 = "asd".ToBoolean(); // true
var boolean6 = "".ToBoolean(); // false
var firstToUpper = "hello world".FirstToUpper(); // Hello world
var firstToUpperRestLower = "hello world".FirstToUpperRestLower(); // Hello world
var lastToUpper = "hello world".LastToUpper(); // hello WorlD
var lastToLower = "hello worLD".LastToLower(); // hello worLd
var longString = "Hello World,Hello World,Hello World";
var truncatedString = longString.Truncate(10); // Hello Worl...
var removedLineEndings = "Hello World\nHello World".RemoveLineEndings(); // Hello WorldHello World
var toHexString = "Hello World".ToHexString(); // 48656C6C6F20576F726C64
var fromHexString = "48656C6C6F20576F726C64".FromHexString(); // Hello World
var toBase64String = "Hello World".ToBase64String(); // SGVsbG8gV29ybGQ=
var fromBase64String = "SGVsbG8gV29ybGQ=".FromBase64String(); // Hello World
var replaceReverse = "Hello World, oldValue".Replace_Reverse("newValue", "oldValue"); // Hello World,
var secureString1 = "Hello World".ToSecureString(); // System.Security.SecureString
var secureString2 = secureString1.ToInsecureString(); // Hello World
var secureStringsEqual = secureString1.SecureStringsEqual(secureString1); // true
var dateString = "2021-01-01";
var isValidDateTime = dateString.IsValidDateTime(); // true
Byte Extensions
var bytes = new byte[] { 1, 2, 3, 4, 5 };
var hexString = bytes.ToHexString(); // 0102030405
var base64String = bytes.ToBase64String(); // AQIDBAU=
var compared = bytes.Compare(bytes); // true
Claims Extensions
var claims = new List<Claim>
{
new Claim("name", "John"),
new Claim("surname", "Doe")
};
var asDictionary = claims.ToDictionary(); // Dictionary<string, string>
var user = new
{
Name = "John",
Surname = "Doe"
};
var claimsIdentity = user.ToClaimsIdentity(); // System.Security.Claims.ClaimsIdentity
var claimsIdentity2 = user.ToClaimsIdentity(out List<Exception> exceptions); // System.Security.Claims.ClaimsIdentity
DateTime extensions
var dateTime = new DateTime(2021, 01, 01);
var nullableDateTime = (DateTime?)dateTime;
var inFuture = dateTime.InFuture(); // false
var inPast = dateTime.InPast(); // true
var notNullableDatetime = nullableDateTime.GetValueOrDefault(); // DateTime
var unixTime = dateTime.ToUnixTime(); // 1609459200
var isDayOlder = dateTime.IsDayOlder(dateTime, 1); // true
var isMinutesOlder = dateTime.IsMinutesOlder(dateTime, 1); // true
var isSecondsOlder = dateTime.IsSecondsOlder(dateTime, 1); // true
var isHoursOlder = dateTime.IsHoursOlder(dateTime, 1); // true
var isMonthsOlder = dateTime.IsMonthsOlder(dateTime, 1); // true
var isYearsOlder = dateTime.IsYearsOlder(dateTime, 1); // true
var readableDateString = dateTime.ToReadableDateString(); // 2 years and 1 month ago
Object Extensions
var user = new
{
Name = "John",
Surname = "Doe"
};
var userDto = user.As<UserDto>(x => {
x.Name = user.Name;
x.Surname = user.Surname;
});
var isValidModel = user.IsValidModel(); // true
var isNull = user.IsNull(); // false
var isNotNull = user.IsNotNull(); // true
var isDefault = user.IsDefault(); // false
Bool Extensions
var nullableBool = (bool?)true;
var notNullableBool = nullableBool.GetValueOrDefault(); // true
Dictionary Extensions
var dictionary = new Dictionary<string, string>
{
{ "name", "John" },
{ "surname", "Doe" }
};
var asQueryString = dictionary.ToQueryString(); // ?name=John&surname=Doe
var asObject = dictionary.ToObject<User>(); // User object with name and surname properties
HttpContext Extensions
var httpContext = new HttpContextAccessor().HttpContext; // Microsoft.AspNetCore.Http.HttpContext
var headerValuesAsDictionary = httpContext.Request.GetHeaderValues(); // Dictionary<string, string>
var xForwardedForOrRemoteIp = httpContext.Request.GetXForwardedForOrRemoteIp(); //
var headerIpAddressList = httpContext.Request.GetHeaderIpAddressList(); // string[]
var remoteIpAddress = httpContext.Request.GetRemoteIpAddress(); // string
var userAgent = httpContext.Request.GetUserAgent(); // string
var isLocal = httpContext.Request.IsLocal(); // true
var query = httpContext.Request.GetQuery(); // Dictionary<string, string>
var queryValue = httpContext.Request.GetQueryValue("name"); // John
var queryAsDictionary = httpContext.Request.GetQueryAsDictionary(); // Dictionary<string, string>
List Extensions
var list = new List<string> { "John", "Jane" };
list.AddIfNotExists("John"); // false
list.AddIfNotExists("Doe"); // true
list.ForEach(x => Console.WriteLine(x)); // John, Doe
var joinString = list.JoinString(","); // John, Doe
var foreachResult = list.ForEachResult<bool>(x => {
var isJohn = x == "John";
return isJohn;
}); // List<bool>
var dataTable = list.ToDataTable(); // System.Data.DataTable
var selectRandom = list.SelectRandom(); // John or Doe
var shuffle = list.Shuffle(); // List<string>
var asObjectList = list.ToObjectList(); // List<object>
list.UpdateAll(x => {
x += " Doe";
}); // John Doe, Jane Doe
list.UpdateAllWhere(x => x.EndsWith("n")), x => {
x += " Doe";
}); // John Doe, Jane Doe
Number Extensions
var dateTime = 1609459200.UnixTimeStampToDateTime(); // 2021-01-01 00:00:00
var dateTime2 = 1609459200.TicksToDateTime(); // 2021-01-01 00:00:00
var isBetween = 5.IsBetween(1, 10); // true
var isBetweenOrEqual = 5.IsBetweenOrEqual(1, 10); // true
var isInRange = 5.IsInRange(1, 10); // true
var nullableInt = (int?)5;
var notNullableInt = nullableInt.GetValueOrDefault(); // 5
var nullableLong = (long?)5;
var notNullableLong = nullableLong.GetValueOrDefault(); // 5
Json Extensions
var user = new User
{
Name = "John",
Surname = "Doe"
};
var json = user.ToJsonString(); // {"Name":"John","Surname":"Doe"}
var user2 = json.FromJsonString<User>(); // User object with name and surname properties
Xml Extensions
var xElement = new XElement("User", new XElement("Name", "John"), new XElement("Surname", "Doe"));
var xElementList = new List<XElement>(){
xElement,
xElement
}
xmlDeserialize = xElement.XmlDeserialize<User>(); // User object with name and surname properties
var xmlDeserializeList = xElementList.XmlDeserialize<List<User>>(); // List<User> object with name and surname properties
var userObject = new User
{
Name = "John",
Surname = "Doe"
};
var asXml = userObject.ToXmlString(); // <User><Name>John</Name><Surname>Doe</Surname></User>
var cleanXmlString = asXml.ToCleanXmlString(); // <User><Name>John</Name><Surname>Doe</Surname></User>
var asXElement = userObject.ToXElement(); // <User><Name>John</Name><Surname>Doe</Surname></User>
var asXElementPropertiesAsAttributes = userObject.ToXElement(true); // <User Name="John" Surname="Doe" />
var asXElementPropertiesAsElement = userObject.ToXElement(false); // <User><Name>John</Name><Surname>Doe</Surname></User>
Hash Extensions
var md5Byte = "text".MD5Hash(); // byte[]
var md5ByteSalted = "text".MD5Hash("salt"); // byte[]
var sha256Byte = "text".SHA256Hash(); // byte[]
var sha256ByteSalted = "text".SHA256Hash("salt"); // byte[]
var sha512Byte = "text".SHA512Hash(); // byte[]
var sha512ByteSalted = "text".SHA512Hash("salt"); // byte[]
var xxHashByte = "text".XXHash(); // byte[]
var filePath = "C:\\Users\\John\\Desktop\\test.txt";
var md5File = filePath.FileMD5Hash(); // byte[]
var xxFile = filePath.FileXXHash(); // byte[]
Validation Extensions
var isValidEmail = "mail@mail.com".IsValidEmail(); //true
var isValidIp = "".IsValidIpAddress(); //false
var isValidIp2 = "11.11.11.11".IsValidIpAddress(out IpAddress ipAddress); //true
//Validates full file path with extension. If given file path is relative path it will be false
var isValidFilePath = "\\Test.txt".IsValidFilePath(); //false
var isValidMacAddress = "00-00-00-00-00-00".IsValidMacAddress(); //true
var isValidPort = 8080.IsValidPort(); //true
var hasSpecialCharacters = "Test".ContainsSpecialChars(); //false
var isStrongPassword = "Test123!".IsStrongPassword(
"!+'-", //Allowed special characters
4, //Minimum length
32, //Maximum length
1, //Minimum number of uppercase letters
1, //Minimum number of lowercase letters
1, //Minimum number of digits
1 //Minimum number of special characters
);
var isUrlImage = "https://www.test.com/test.jpg".IsUrlImage(); //true
var isValidUrl = "https://www.test.com".IsValidUrl(); //true
var isValidConnectionString = "Server=.;Database=Test;Trusted_Connection=True;".IsValidConnectionString(); //true
var isValidCreditCard = "1111-1111-1111-1111".IsValidCreditCard("01/01",999); //true
DgcAPI
Every method has a token and timeout parameter as optional. Depending on the http request method a body is required.
var dummyUrl = "https://127.0.0.1/";
var dummyAuthorizationToken = "123123213";
var get = DgcAPI.Get(dummyUrl); // HttpResponseMessage
var getWithAuthorization = DgcAPI.Get(dummyUrl,dummyAuthorizationToken); // HttpResponseMessage
var getWithAuthorizationAndTimeOut = DgcAPI.Get(dummyUrl,dummyAuthorizationToken,10); // HttpResponseMessage
var getAndReadJson = DgcAPI.GetAndReadJson(dummyUrl); // JObject
var postAsJsonAndReadJson = DgcAPI.PostAsJsonAndReadJson(dummyUrl, new { Name = "John" }); // JObject
var postAsJson = DgcAPI.PostAsJson(dummyUrl, new { Name = "John" }); // HttpResponseMessage
var delete = DgcAPI.Delete(dummyUrl); // HttpResponseMessage
var putAsJson = DgcAPI.PutAsJson(dummyUrl, new { Name = "John" }); // HttpResponseMessage
var send = DgcAPI.Send(dummyUrl, new HttpRequestMessage(),dummyAuthorizationToken, 10); // HttpResponseMessage
DgcCache
Simple memory caching
List<User> GetUsersFromDb(){ /* db access */}
var dgcCache = new DgcCache<List<User>>(GetUsersFromDb, 10); // 10 minutes
var users = dgcCache.Get(); // List<User>
Caching with key and value
User GetUserFromDb(int userId){ /* db access */}
var dgcCache = new DgcCache<int ,List<User> >(GetUserFromDb, 10); // 10 minutes
var user = dgcCache.Get(231); // Nullable<User>
DgcCheck
var password = "123123";
var passwordScore = DgcCheck.CheckPasswordStrength(password); // PasswordScore Enum
PasswordScore enum
public enum PasswordScore
{
Blank = 0,
VeryWeak = 1,
Weak = 2,
Medium = 3,
Strong = 4,
VeryStrong = 5
}
DgcConfig
Dgcily access app.config or web.config
var connString = DgcConfig.GetConnectionString("DefaultConnection"); // string
var someValue = DgcConfig.GetValue("someValue"); // string
var someIntValue = DgcConfig.GetValue<int>("someIntValue"); // int
DgcDirectory
Get known folder paths
var downloadsPath = DgcDirectory.GetPath(KnownFolder.Downloads); // string
var contactsPath = DgcDirectory.GetPath(KnownFolder.Contacts); // string
var favoritesPath = DgcDirectory.GetPath(KnownFolder.Favorites); // string
var linksPath = DgcDirectory.GetPath(KnownFolder.Links); // string
var savedGamesPath = DgcDirectory.GetPath(KnownFolder.SavedGames); // string
var savedSearchesPath = DgcDirectory.GetPath(KnownFolder.SavedSearches); // string
KnownFolder enum
public enum KnownFolder
{
Contacts,
Downloads,
Favorites,
Links,
SavedGames,
SavedSearches
}
DgcEncrypt
Encryption and decryption with Aes algorithm
Startup.cs configuration
DgcEncrypt.SetKey("SuperSecretEncryptionKey");
//Only if you want to use time seeding, see below
DgcEncrypt.UseTimeSeeding(DgcEncrypt.Sensitivity.Minutes);
//Only if debug build
DgcEncrypt.DontUseEncryption();
Usage
var encryptor = DgcEncrypt.Create();
var encrypted = encryptor.Encrypt("123123"); // encrypted string
var decrypted = encryptor.Decrypt(encrypted); // 123123
Time seeding
Time seeding creates a salt key based on the time the instance is created. You can set the sensitivity of the time seeding when enabling time seeding with DgcEncrypt.UseTimeSeeding method. If sensitivity set to seconds, the salt key will change every second. Meaning the encrypted text will also change. Created salt key will be stored inside of the instance. You can use same instance to decrypt the encrypted text, even after the time has changed.
WARNING: If you want to store the encrypted text in a database and decrypt it later, be aware that the salt key might change and you may not be able to recover the original text.
Static seed
Static seed is used to alter the time seeded salt key. You can set the static seed with DgcEncrypt.UseTimeSeeding method. This mostly set to project build number so every build will have a different salt key. Default value is set to 0, if you have not set it, it will not be used.
DgcFile
var sourcePath = @"C:\Users\John\Desktop\test.txt";
var destinationPath = @"C:\Users\John\Desktop\test2.txt";
var sourceDirectoryPath = @"C:\Users\John\Desktop\test";
DgcFile.DeleteAll(destinationPath); // Deletes all files and directories inside of the destination path
DgcFile.CopyAll(sourcePath, destinationPath); // Copies the file or directory from source to destination
DgcFile.MoveAll(sourcePath, destinationPath); // Moves the file or directory from source to destination
// Deletes all files and directories inside of the source path where the name contains "test"
DgcFile.DeleteDirectoryWhere(sourceDirectoryPath,x => x.Name.Contains("test"), true);
// Deletes all files inside of the source path where the name contains "test"
DgcFile.DeleteFileWhere(sourceDirectoryPath,x => x.Name.Contains("test"));
var fileInfo1 = new FileInfo(sourcePath); // FileInfo
var fileInfo2 = new FileInfo(destinationPath); // FileInfo
//Reads 64 bytes on each loop till the end of the file to compare
var filesAreEqual = DgcFile.FilesAreEqual(fileInfo1, fileInfo2); // bool
//Reads 1 byte on each loop till the end of the file to compare
var filesAreEqualOneByte = DgcFile.FilesAreEqual_OneByte(fileInfo1, fileInfo2); // bool
//Reads all bytes at once and hashes them with MD5 to compare
var filesAreEqualMD5Hash = DgcFile.FilesAreEqual_MD5Hash(fileInfo1, fileInfo2); // bool
//Reads all bytes at once and hashes them with xxHash to compare
var filesAreEqualXXHash = DgcFile.FilesAreEqual_XXHash(fileInfo1, fileInfo2); // bool
DgcGenerate
const string DefaultCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //Hardcoded
var randomString = DgcGenerate.RandomString(10); // Random string with 10 characters
var randomInt = DgcGenerate.RandomInt(100, 200); // Random int between 100 and 200
var randomNumber = DgcGenerate.RandomNumber(100); // Random number with 100 digits
var randomString1 = DgcGenerate.RandomString(10, true); // Random string without digits
var randomString1 = DgcGenerate.RandomString(10, true, "'^%!"); // Random string without digits and with custom characters
DgcHttp
var statusCodeShortMessage = DgcHttp.GetStatusCodeShortMessage(200); // OK
var statusCodeLongMessage = DgcHttp.GetStatusCodeLongMessage(500); // Internal Server Error
DgcINI
You can read and write to .ini files with DgcINI class. Also you can parse the .ini file to IniFile model.
var iniFile = DgcINI.Parse("C:\\Users\\John\\Desktop\\test.ini");
var dgcIni = new DgcINI(iniFile);
dgcIni.Write("Section", "Key", "Value");
var value = dgcIni.Read("Section", "Key");
var iniFile = DgcINI.ParseByPath(iniFile);
var str = iniFile.WriteToString();
iniFile.WriteToPath(iniFile);
IniFile models
public class IniSection
{
public string? Name { get; set; }
public List<IniData> Data { get; set; } = new();
public List<IniComment> Comments { get; set; } = new();
}
public class IniData
{
public string? Key { get; set; }
public string? Value { get; set; }
}
public class IniComment
{
public int LineNo { get; set; } = -1;
public string? Comment { get; set; }
}
DgcJWT
var dgcJwt = new DgcJWT("Secret","Issuer","Audience");// or new DgcJWT("Secret");
var claimsIdentity = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, "John"),
new Claim(ClaimTypes.Role, "Admin"),
new Claim(ClaimTypes.Role, "User"),
});
var token = dgcJwt.GenerateToken(claimsIdentity,60); // Generates token with claims for 60 minutes
var claimsDictionary = new Dictionary<string, object>
{
{"Name", "John"},
{"Role", "Admin"},
{"Role", "User"},
};
var token2 = dgcJwt.GenerateToken(claimsDictionary,60); // Generates token with claims for 60 minutes
// Validates token and returns ClaimsPrincipal, if token is expired or is not valid it throws exception.
var claimsPrincipal = dgcJwt.ValidateToken(token);
DgcMail
//Create DgcMail instance with smtp server, mail address, password, port and ssl
var dgcEmail = new DgcMail("smtp.gmail.com", "mailAddress", "password", 57, true);
dgcEmail.Send("Subject","Body","mailAddress", true); // Sends mail to mailAddress with html body
dgcEmail.Send("Subject","Body","mailAddress", false); // Sends mail to mailAddress
DgcMemoryCache
Improved caching from DgcCache. Both are viable and working the way how caching and receiving data is different. This is a singleton class without any service or injection required.
DgcMemoryCache.This.Set("Key", "Value", 60); // Sets cache for 60 seconds
var value = DgcMemoryCache.This.Get("Key"); // Gets cache value
var valueStr = DgcMemoryCache.This.Get<string>("Key"); // Gets cache value as string
var keyExists = DgcMemoryCache.This.Exists("Key"); // Checks if cache exists
DgcMemoryCache.This.Remove("Key"); // Removes cache
DgcMemoryCache.This.Clear(); // Clears all cache
// Gets cache value or sets it if it doesn't exist and then returns it
var value = DgcMemoryCache.This.GetOrSet("Key", () => "Value", 60);
DgcQL
Simple and dgcy to use SQL database access. This is not related to EntityFrameworkCore it uses Microsoft.Data.SqlClient. You can use static methods or create an instance of DgcQL class and pass connection string. Here is an example of creating instance. When using static methods connection string must be passes everytime.
var dgcQL = new DgcQL("Server=.;Database=Test;Trusted_Connection=True;");
var command = new SqlCommand("SELECT * FROM TestTable");
var result = dgcQL.GetTable(command); //DataTable
var result2 = dgcQL.ExecNonQuery(command); //int
var result3 = dgcQL.ExecNonQueryAsync(command); //Task<int>
var result4 = dgcQL.ExecScalar(command); //object
var result5 = dgcQL.ExecScalarAsync(command); //Task<object>
dgcQL.BackupDatabase("C:\\Users\\John\\Desktop\\Test.bak", "DbName"); // Backups database
dgcQL.BackupDatabaseAsync("C:\\Users\\John\\Desktop\\Test.bak", "DbName"); // Backups database and returns Task
dgcQL.ShrinkDatabase("DbName","DbLogFileName");
dgcQL.TruncateTable("TableName");
dgcQL.TruncateTableAsync("TableName"); // Returns Task
dgcQL.DropTable("TableName");
dgcQL.DropTableAsync("TableName"); // Returns Task
var tables = dgcQL.GetAllTableNames();
DgcReCaptcha
//validates google captcha
var secret = "Secret";
var captchaResponse = "Response";
var result = DgcReCaptcha.Validate(secret, captchaResponse);
Captcha validation result model
public class CaptchaResponseModel
{
public bool Success { get; set; } = false;
public DateTime ChallengeTS { get; set; }
public string ApkPackageName { get; set; }
public string ErrorCodes { get; set; }
}
DgcTask
A task manager that can run tasks in background in a queue.
var dgcTask = new DgcTask();
dgcTask.AddTask(() => Console.WriteLine("Task 1"));
dgcTask.AddTask(() => Console.WriteLine("Task 2"));
dgcTask.AddTask(() => Console.WriteLine("Task 3"));
//On Application Exit or wait all tasks to finish
dgcTask.Flush();
//Disposes dgcTask and all the related tasks and threads
//This method will not wait for tasks to be ended
dgcTask.Dispose();
DgcZip
var fileList = new List<string>();
var zipFile = "C:\\Users\\John\\Desktop\\Test.zip";
DgcZip.MakeZip(fileList, zipFile); // Makes zip file from fileList
DgcZip.UnZip(zipFile, "C:\\Users\\John\\Desktop\\Test"); // Unzips zipFile to destination
DgcLog
Using logger
_logger.Trace("Hello World!");
_logger.Debug("Hello World!");
_logger.Info("Hello World!");
_logger.Warn("Hello World!");
_logger.Error("Hello World!");
_logger.Fatal("Hello World!");
_logger.Fatal(new Exception(),"Hello World!");
_logger.Exception(new Exception(),"Hello World!");
Accessing static logger
DgcLogFactory.StaticLogger.Trace("Hello World!");
On application exit
DgcLog.Flush();
DgcLogConsole
Static class for logging to console with colors
DgcLogConsole.Log("Hello World!",ConsoleColor.White);
DgcLogConsole.Log(DgcLogLevel.Warning,"Hello World!");
DgcLogConsole.Trace("Hello World!");
DgcLogConsole.Debug("Hello World!");
DgcLogConsole.Info("Hello World!");
DgcLogConsole.Warn("Hello World!");
DgcLogConsole.Error("Hello World!");
DgcLogConsole.Fatal("Hello World!");
Log Configuration
Configure logger in startup
DgcLogFactory.Configure(x =>
{
x.WebInfoLogging = true;
x.MinimumLogLevel = DgcLogLevel.Information;
x.LogFileName = "Log_";
x.ExceptionHideSensitiveInfo = false;
});
Configuration class properties
/// <summary>
/// Gets or sets a value indicating whether to log the request body.
/// </summary>
public DgcLogLevel MinimumLogLevel { get; set; } = DgcLogLevel.Information;
/// <summary>
/// Set logs folder path to be stored. Defualt is current directory, adds folder named Logs.
/// </summary>
public string LogFolderPath { get; set; } = ".\\Logs";
/// <summary>
/// Formatting DateTime in log file name, default value is "MM.dd.yyyy".
/// This is added after LogFileName variable.
/// </summary>
public string DateFormatString { get; set; } = "MM.dd.yyyy";
/// <summary>
/// Set log file name, default value is "Log_".
/// This will what value to write before datetime.
/// </summary>
public string LogFileName { get; set; } = "Log_";
/// <summary>
/// Set log file extension default value is ".json".
/// </summary>
public string LogFileExtension { get; set; } = ".json";
/// <summary>
/// Whether to enable logging to console, writes json logs in console as well as saving logs to a file.
/// Default value is true.
/// </summary>
public bool ConsoleAppender { get; set; } = true;
/// <summary>
/// Whether to log incoming web request information to log file, default value is false.
/// If your app running on server this should be set to true.
/// Also you need to Configure DgcMe.HttpContext in order to log request data.
/// </summary>
public bool WebInfoLogging { get; set; } = false;
/// <summary>
/// Whether to hide sensitive information from being logged.
/// Default value is true.
/// If set to false, it will log sensitive information.
/// Otherwise it will only print the exception message.
/// </summary>
public bool ExceptionHideSensitiveInfo { get; set; } = true;
public bool SeparateLogLevelToFolder { get; set; } = false;
DgcLogReader
Reading logs from log files
//Create instance
var logReader = new DgcLogReader();
//Read all logs
var logs = logReader.GetLogs(); //IEnumerable<LogModel>
LogModel properties
public DateTime Date { get; } = DateTime.Now;
public DgcLogLevel Level { get; set; }
public string? Source { get; set; }
public object? Log { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public CleanException? Exception { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public WebInfo? WebInfo { get; set; }
DgcMe.Result
DgcMe.Result is a simple library for creating result objects. It is a lightweight library with no dependencies. It offers a simple way to create result objects with data, error messages, exceptions, validation errors and severity.
Why would you use it?
Throwing exceptions and handling them later is not a good practice. Exceptions are not performance friendly and should be used only when absolutely necessary. Result objects are a good way to handle errors without throwing any exceptions.
Result
Creating result
Result.Create(isSuccess: true,
severity: ResultSeverity.Success
errorCode: "UserNotExists",
errors: new List<string>(),
validationErrors: new List<ValidationError>());
//Using error codes
Result.Success("UserUpdateSuccess");
///Using ErrorCode as messages
Result.Success("User updated successfully");
//Various severities
Result.Warn("UserUpdateWarning");
Result.Error("UserUpdateWarning");
Result.Fatal("UserUpdateWarning");
//Built-in errors
Result.NotFound();
Result.Unauthorized();
Result.Forbidden();
//Creating result with multiple errors
Result.Error("Error",new List<string> { "Error1", "Error2" });
Result.Error("Error","NotFound","NotValid");
//Converting exceptions to result
Result.Exception(new Exception("Error"));
//Creating validation errors
Result.ValidationError(new ValidationError("Username length must be at ldgct 3 characters"))
Result.ValidationError(
new ValidationError("Username length must be at ldgct 3 characters"),
new ValidationError("Password length must be at ldgct 3 characters","Password"),//Password is property name
new ValidationError("Password must contain at ldgct one number","Password","PasswordIsShort")); //PasswordIsShort is error code
);
Method example
public Result UpdateUser(User user){
//Validate user
var isValid = user.Validate();
if(!isValid){
return Result.ValidationError(user.ValidationErrors);
}
var isUserNameShort = user.Username.Length < 3;
if(isUserNameShort){
return Result.ValidationError(new ValidationError("Username length must be at ldgct 3 characters"));
}
//or
if(isUserNameShort){
return Result.Warn("Username length must be at ldgct 3 characters");
}
var dbRowsAffected = _db.Save();
if(dbRowsAffected == 0){
return Result.Error("Error saving user");
}
return Result.Success("User updated successfully");
}
Result with data
ResultData<{T}> is a result object with data property. If data is null result status will be false. There is no create methods for ResultData<{T}>. You can create it by using Result.Warn() method for creating failed results. And implicit operators are implemented for ResultData<{T}>. So you can return {T} from methods and it will automatically converted to ResultData<{T}>.
If you try to convert a Result.Success() with implicit operator ResultData<{T}> it will throw an exception.
Using ResultData<{T}>
public ResultData<User> GetUser(int id){
var exists = _db.Users.Any(x => x.Id == id);
if(!exists){
return Result.Warn("User not found");
//or creating with errorCode and errorMessages
//in this example we are using errorMessages as parameters to errorCode
//later we can create and show error messages based on errorCode in localization file
return Result.NotFound("NotFound", "User");
//or creating as validation error
return Result.ValidationError("User not found", "User", "NotFound");
}
var user = _db.Users.FirstOrDefault(x => x.Id == id);
//User automatically converted to ResultData<User> with data property
//If use is not null result status will be success
//And Severity will be information
//If user is null result status will be error
//And error code will automatically will be set to NullValue
return user;
Converting ResultData to Result
ResultData<User> resultData = Result.Warn("NotFound"); //ResultData<User>
var result = resultData.ToResult(); //Result
Converting Result or ResultData to ActionResult
If result status is success it will return OkObjectResult. If result status is error and FailStatusCode is not set it will return BadRequestObjectResult
ResultData<User> resultData = Result.Warn("NotFound"); //ResultData<User>
var result = resultData.ToActionResult(); //ActionResult
var result2 = resultData.ToActionResult(400); //ActionResult with 400 status code
var result3 = Result.Warn("NotFound").ToActionResult(): //ActionResult
Mapping ResultData
var getUserResult = GetUser(1); //ResultData<User>
var user = getUserResult.Map<UserDto>(x => {
return new UserDto{
Id = x.Id,
Username = x.Username
};
});
Result Severity Enum
public enum ResultSeverity
{
None,
Info,
Validation,
Warn,
Error,
Exception,
Fatal
}
DgcMe.System
Getting device information
Creating unique id
Adding application to windows startup
DgcMe.Test
This Console Application project is for testing newly added functionality. Not a unit test project.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- DgcMe (>= 1.0.2)
- DgcMe.Result (>= 1.0.2)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.