Simple text logging for a C# console application

Simple logging to text file into a directory called ‘logs’ inside the directory of the executing image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private static object objLock = new object();

public static void LogInfo(string d)
{
var sb = new StringBuilder();
sb.AppendFormat("INFO : {0} {1}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"), Environment.NewLine);
sb.Append(d);
Log(sb.ToString());
}

public static void LogError(Exception ex)
{
var sb = new StringBuilder();
sb.AppendFormat("EXCEPTION : {0} {1}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"), Environment.NewLine);
sb.AppendFormat("{0}{1}", ex.Message, Environment.NewLine);
sb.AppendFormat("{0}{1}", ex.StackTrace, Environment.NewLine);
Log(sb.ToString());
}

static void Log(string d)
{
Console.WriteLine(d);
var _logsDir = "logs";

if (!Directory.Exists(_logsDir))
Directory.CreateDirectory(_logsDir);

lock (objLock)
{
var path = Path.Combine("logs", LogFileName());
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine(d);
sw.WriteLine(" ");
sw.Close();
}
}
}

static string LogFileName()
{
return DateTime.Now.ToString("ddMMyyyy") + ".log";
}

Pillars Of Object Oriented Programming (OOP)

Everything in life that is structured will follow a blue print or pattern that is based on proven design methods whether they are concept based or concrete. That is to say the result is tangible.

Software development regardless of language too has patterns and principles which if effectively applied to your solutions will result in code that is maintainable, testable and ultimately flexible enough to change as scope or business requirements evolve.

Always remember that software development is an iterative process, your job is never done.

These days we as software developers are spoilt with access to information on the internet detailing different design patterns and principles. It would be very presumptuous for any developer to assume they understand every pattern or principle however there are some foundational points that I personally feel every developer should be au fait with or at very least have a functional understanding thereof.

Enter the Pillars of OOP.

Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.

Pillars of OOP

In all literature I have seen surrounding object orientated programming these 3 points always come up and are the foundation or “Pillars” of Object Orientated Programming. I share the personal view of Scott Allen that encapsulation is the primary pillar as without encapsulation we could never build applications on a large scale.

https://app.pluralsight.com/profile/author/scott-allen

Encapsulation is the primary pillar of object-oriented programming, and most classes and training tend to overemphasize inheritance and polymorphism as being more useful than they really in day to day programming. ~ Scott Allen

Encapsulation

Encapsulation is simply data hiding where by your classes encapsulate their internal state and complexity, this means when building your solution you only need to know about your current cognitive load as the encapsulated state will look after itself. The only access to the state (the data) would be though well-defined functions on the class or property setters. The constructor is responsible for setting the property values on instantiation so they are in a useful state when you go looking for them.

CLASS EXAMPLE

Inheritance

Class inheritance allows developers to create new classes that reuse behaviour (functions and properties) from their base classes. The class that is inheriting from a base class (or classes) is called a derived class.

Inheritance leverages re-use of existing code classes, this results in code that is easier to maintain and faster to develop.

CLASS EXAMPLE

Polymorphism

If the word is broken in half; “Poly” – means many and “Morphism” – means to change. So “Polymorphism” means having many forms. In OOP polymorphism can be expressed as ‘One interface, multiple functions.’

CLASS EXAMPLE

Building on the Pillars

The following are key points that build on our Pillars of OOP and are of critical importance to produce solutions that are of high quality.

Abstraction

Abstraction is not a pattern but rather a principle and many developers don’t consider abstraction a pillar however this is opinion based. I am of the opinion that it’s a pillar as classes with high cohesion are far simpler to test and maintain.

In computer programming, cohesion refers to the degree to which the elements inside a module belong together.

Abstraction in terms of class files is to simplify them to have a high level of cohesion. Simply put this means the properties and methods directly relate to the class you are building.

CLASS EXAMPLE

References