<span class="token keyword keyword-using">using</span> <span class="token namespace">System</span><span class="token punctuation">;</span>
<span class="token keyword keyword-using">using</span> <span class="token namespace">System<span class="token punctuation">.</span>Data<span class="token punctuation">.</span>SqlClient</span><span class="token punctuation">;</span>
<span class="token keyword keyword-using">using</span> <span class="token namespace">System<span class="token punctuation">.</span>Configuration</span><span class="token punctuation">;</span>
<span class="token keyword keyword-public">public</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-class">class</span> <span class="token class-name">Logger</span>
<span class="token punctuation">{</span>
<span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-readonly">readonly</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> connectionString <span class="token operator">=</span> ConfigurationManager<span class="token punctuation">.</span>ConnectionStrings<span class="token punctuation">[</span><span class="token string">"YourConnectionStringName"</span><span class="token punctuation">]</span><span class="token punctuation">.</span>ConnectionString<span class="token punctuation">;</span>
<span class="token keyword keyword-public">public</span> <span class="token keyword keyword-static">static</span> <span class="token return-type class-name"><span class="token keyword keyword-void">void</span></span> <span class="token function">LogError</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> ex<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> userId <span class="token operator">=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> additionalInfo <span class="token operator">=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token function">Log</span><span class="token punctuation">(</span><span class="token string">"Error"</span><span class="token punctuation">,</span> ex<span class="token punctuation">.</span>Message<span class="token punctuation">,</span> ex<span class="token punctuation">.</span>StackTrace<span class="token punctuation">,</span> ex<span class="token punctuation">.</span>Source<span class="token punctuation">,</span> userId<span class="token punctuation">,</span> additionalInfo<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword keyword-public">public</span> <span class="token keyword keyword-static">static</span> <span class="token return-type class-name"><span class="token keyword keyword-void">void</span></span> <span class="token function">LogWarning</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> message<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> userId <span class="token operator">=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> additionalInfo <span class="token operator">=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token function">Log</span><span class="token punctuation">(</span><span class="token string">"Warning"</span><span class="token punctuation">,</span> message<span class="token punctuation">,</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">,</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">,</span> userId<span class="token punctuation">,</span> additionalInfo<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword keyword-public">public</span> <span class="token keyword keyword-static">static</span> <span class="token return-type class-name"><span class="token keyword keyword-void">void</span></span> <span class="token function">LogInfo</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> message<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> userId <span class="token operator">=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> additionalInfo <span class="token operator">=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token function">Log</span><span class="token punctuation">(</span><span class="token string">"Info"</span><span class="token punctuation">,</span> message<span class="token punctuation">,</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">,</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">,</span> userId<span class="token punctuation">,</span> additionalInfo<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token return-type class-name"><span class="token keyword keyword-void">void</span></span> <span class="token function">Log</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> logLevel<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> message<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> stackTrace<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> source<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> userId<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> additionalInfo<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword keyword-try">try</span>
<span class="token punctuation">{</span>
<span class="token keyword keyword-using">using</span> <span class="token punctuation">(</span><span class="token class-name">SqlConnection</span> conn <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">SqlConnection</span><span class="token punctuation">(</span>connectionString<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
conn<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name"><span class="token keyword keyword-string">string</span></span> query <span class="token operator">=</span> <span class="token string">@"
INSERT INTO ErrorLogs (LogDate, LogLevel, Message, StackTrace, Source, UserId, AdditionalInfo)
VALUES (@LogDate, @LogLevel, @Message, @StackTrace, @Source, @UserId, @AdditionalInfo)"</span><span class="token punctuation">;</span>
<span class="token keyword keyword-using">using</span> <span class="token punctuation">(</span><span class="token class-name">SqlCommand</span> cmd <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">SqlCommand</span><span class="token punctuation">(</span>query<span class="token punctuation">,</span> conn<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
cmd<span class="token punctuation">.</span>Parameters<span class="token punctuation">.</span><span class="token function">AddWithValue</span><span class="token punctuation">(</span><span class="token string">"@LogDate"</span><span class="token punctuation">,</span> DateTime<span class="token punctuation">.</span>Now<span class="token punctuation">)</span><span class="token punctuation">;</span>
cmd<span class="token punctuation">.</span>Parameters<span class="token punctuation">.</span><span class="token function">AddWithValue</span><span class="token punctuation">(</span><span class="token string">"@LogLevel"</span><span class="token punctuation">,</span> logLevel<span class="token punctuation">)</span><span class="token punctuation">;</span>
cmd<span class="token punctuation">.</span>Parameters<span class="token punctuation">.</span><span class="token function">AddWithValue</span><span class="token punctuation">(</span><span class="token string">"@Message"</span><span class="token punctuation">,</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span>
cmd<span class="token punctuation">.</span>Parameters<span class="token punctuation">.</span><span class="token function">AddWithValue</span><span class="token punctuation">(</span><span class="token string">"@StackTrace"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword keyword-object">object</span><span class="token punctuation">)</span>stackTrace <span class="token operator">??</span> DBNull<span class="token punctuation">.</span>Value<span class="token punctuation">)</span><span class="token punctuation">;</span>
cmd<span class="token punctuation">.</span>Parameters<span class="token punctuation">.</span><span class="token function">AddWithValue</span><span class="token punctuation">(</span><span class="token string">"@Source"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword keyword-object">object</span><span class="token punctuation">)</span>source <span class="token operator">??</span> DBNull<span class="token punctuation">.</span>Value<span class="token punctuation">)</span><span class="token punctuation">;</span>
cmd<span class="token punctuation">.</span>Parameters<span class="token punctuation">.</span><span class="token function">AddWithValue</span><span class="token punctuation">(</span><span class="token string">"@UserId"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword keyword-object">object</span><span class="token punctuation">)</span>userId <span class="token operator">??</span> DBNull<span class="token punctuation">.</span>Value<span class="token punctuation">)</span><span class="token punctuation">;</span>
cmd<span class="token punctuation">.</span>Parameters<span class="token punctuation">.</span><span class="token function">AddWithValue</span><span class="token punctuation">(</span><span class="token string">"@AdditionalInfo"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword keyword-object">object</span><span class="token punctuation">)</span>additionalInfo <span class="token operator">??</span> DBNull<span class="token punctuation">.</span>Value<span class="token punctuation">)</span><span class="token punctuation">;</span>
cmd<span class="token punctuation">.</span><span class="token function">ExecuteNonQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword keyword-catch">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment">// Handle any exceptions that occur while trying to log the error (e.g., send an email, write to a file, etc.)</span>
Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">"Logging failed: "</span> <span class="token operator">+</span> e<span class="token punctuation">.</span>Message<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>