Hi
How to create dynamic error based log information . Whenever error is generated information is stored in log table .
Thanks
Hi
How to create dynamic error based log information . Whenever error is generated information is stored in log table .
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Aman GuptaRank #193 · 12.4K reputationPosted Aug 22, 2024, 1:57 AM EDT
Hi Ramco,
To create dynamic error-based log information in .NET and store it in a database table, you can follow these steps:
1. Create a Log Table in the Database
First, set up a table in your database to store log information. Here's an example of what the table might look like:
CREATE TABLE ErrorLogs ( LogId INT PRIMARY KEY IDENTITY, LogDate DATETIME NOT NULL, LogLevel VARCHAR(50) NOT NULL, Message NVARCHAR(MAX) NOT NULL, StackTrace NVARCHAR(MAX), Source NVARCHAR(255), UserId NVARCHAR(50), AdditionalInfo NVARCHAR(MAX) );2. Create a Logging Class in .NET
Create a class to handle the logging of errors to the database. This class will include methods to log different levels of information (e.g., errors, warnings, info).
using System; using System.Data.SqlClient; using System.Configuration; public static class Logger { private static readonly string connectionString = ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString; public static void LogError(Exception ex, string userId = null, string additionalInfo = null) { Log("Error", ex.Message, ex.StackTrace, ex.Source, userId, additionalInfo); } public static void LogWarning(string message, string userId = null, string additionalInfo = null) { Log("Warning", message, null, null, userId, additionalInfo); } public static void LogInfo(string message, string userId = null, string additionalInfo = null) { Log("Info", message, null, null, userId, additionalInfo); } private static void Log(string logLevel, string message, string stackTrace, string source, string userId, string additionalInfo) { try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); string query = @" INSERT INTO ErrorLogs (LogDate, LogLevel, Message, StackTrace, Source, UserId, AdditionalInfo) VALUES (@LogDate, @LogLevel, @Message, @StackTrace, @Source, @UserId, @AdditionalInfo)"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@LogDate", DateTime.Now); cmd.Parameters.AddWithValue("@LogLevel", logLevel); cmd.Parameters.AddWithValue("@Message", message); cmd.Parameters.AddWithValue("@StackTrace", (object)stackTrace ?? DBNull.Value); cmd.Parameters.AddWithValue("@Source", (object)source ?? DBNull.Value); cmd.Parameters.AddWithValue("@UserId", (object)userId ?? DBNull.Value); cmd.Parameters.AddWithValue("@AdditionalInfo", (object)additionalInfo ?? DBNull.Value); cmd.ExecuteNonQuery(); } } } catch (Exception e) { // Handle any exceptions that occur while trying to log the error (e.g., send an email, write to a file, etc.) Console.WriteLine("Logging failed: " + e.Message); } } }3. Log Errors in Your Application
You can now use this Logger class to log errors whenever they occur in your application.
try { // Your code that might throw an exception } catch (Exception ex) { Logger.LogError(ex, userId: "CurrentUserId", additionalInfo: "Additional context if needed"); }4. Configuration (Optional)
Make sure you have a connection string in your Web.config or App.config file.
5. Customize as Needed
This approach provides a structured way to log and track errors in your application.