With the beginning of the year, I thought it would be time to share our technical experience with you, those who follow us.
Over time, more than once, we found ourselves in the situation of not being able to solve a problem quickly because the problem could not be identified in time.
Clearly, a very fast error identification solution was needed. And here we can choose between two options: the LOG (everyone, in a more or less conscious way, has encountered such files, whether they belong to a custom system or the operating system) or writing errors to the database (or anything else that needs to be tracked).
We will start with the LOG.
The best option would be to create our own library that writes to the LOG what we want; in this way, we only call a method multiple times, without needing to overload our application code.
So the first step: we need to put in the configuration file the location where the log file is located:
<appSettings>
<add key="LogPath" value="LOG" />
</appSettings>
Then we create a new Class Library project where we put the code below:
public void LogForDebug(string message)
{
try
{
string str = LogCreateFolder();
string data = string.Format("{0:yyyyMMdd}", DateTime.Now);
string PathLog = str + "\\" + data + ".txt";
string LogToWrite = DateTime.Now.ToString() + " *** LOG FOR DEBUG - ";
LogToWrite += message;
if (!File.Exists(PathLog))
{
StreamWriter writer = new StreamWriter(PathLog);
writer.Close();
}
using (TextWriter tw = File.AppendText(str))
{
tw.WriteLine(Environment.NewLine);
tw.WriteLine(LogToWrite);
}
}
catch { }
}
The next method is to check if the folder exists. If it does not exist, we create it:
private string LogCreateFolder()
{
string str = ConfigurationManager.AppSettings["LogPath"];
if (!Directory.Exists(str))
Directory.CreateDirectory(str);
return str;
}
And this is how the log can be written.
Writing the LOG to a database
The second option is to write to the database.
We can use the same project created above. The code will differ somewhat because we need to work with the database.
public void LogForDebug(Guid SessionGuid,string message)
{
SqlConnection Conn = DataBaseConnection.Instance.LogConnection();
string Script = "INSERT INTO LOG VALUES ('" + SessionGuid + "','" + message + "')";
try
{
if (CheckLogTable())
{
using (SqlCommand Comm = new SqlCommand(Script, Conn))
{
Conn.Open();
Comm.CommandType = System.Data.CommandType.Text;
Comm.ExecuteNonQuery();
Conn.Close();
}
}
}
catch { }
}
The next method is to check if the LOG table exists in the database. If the table does not exist, we use the LogTableCreate method to create it automatically directly from the code.
private bool CheckLogTable()
{
bool Exists;
const string sqlStatement = @"SELECT COUNT(*) FROM LOG";
SqlConnection conn = DataBaseConnection.Instance.LogConnection();
Try
{
using (SqlCommand cmd = new SqlCommand(sqlStatement, conn))
{
conn.Open();
object o = cmd.ExecuteScalar();
Exists = (o == null ? false : true);
if (!Exists)
{
LogTableCreate();
Exists = true;
}
}
}
Catch
{
LogTableCreate();
Exists = true;
}
return Exists;
}
private void LogTableCreate()
{
using (SqlConnection conn = DataBaseConnection.Instance.LogConnection())
{
conn.Open();
Server srv = new Server(conn.DataSource);
Database db = srv.Databases[conn.Database];
Table tb = new Table(db, "LOG");
Column Id = new Column(tb, "Id", DataType.Int);
Id.Identity = true;
Id.IdentityIncrement = 1;
Column Guid = new Column(tb, "SessionGuid", DataType.UniqueIdentifier);
Column LogMessage = new Column(tb, "LogMessage", DataType.VarCharMax);
tb.Columns.Add(Id);
tb.Columns.Add(Guid);
tb.Columns.Add(LogMessage);
tb.Create();
conn.Close();
}
}
What information do we write in the LOG
In physical LOG files, we can write absolutely all the information we want. Whether it is errors or simply various information.
However, the recommendation is to limit as much as possible the interaction between the interface and the database. Thus, we can limit ourselves exclusively to writing errors.