Skip to content

Commit

Permalink
Log for context and save and clear log options implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
umairsyed613 committed May 1, 2021
1 parent 96ab78f commit f46bbe3
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 43 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ Log.Logger = new LoggerConfiguration()
.CreateLogger();
```

#### Log For Context

SimpleLogTextBox, RichTextBoxLogControl or JsonLogTextBox can be configured to display log for specific ***Context***.
All of the controls have property **For Context** which can be configured with ***namespace.classname*** for example ***TestApplication.Form2*** you can file the sample here >> [Sample](https://github.com/umairsyed613/Serilog.Sinks.WinForms/blob/master/src/Sample/TestApplication/Form2.cs)

**Note:** Remember to configure Logger instance with ***Enrich.FromLogContext()***


#### Save and Clear Logs
SimpleLogTextBox, RichTextBoxLogControl or JsonLogTextBox have two method available for saving log to file or clear the log from the log control.
***ClearLogs()*** AND ***SaveLogToFile()***

Check the usage in sample application here [Sample](https://github.com/umairsyed613/Serilog.Sinks.WinForms/blob/master/src/Sample/TestApplication/Form2.cs)


### Sample Code

Find the sample running application [here](https://github.com/umairsyed613/Serilog.Sinks.WinForms/tree/master/Sample/TestApplication/)
Expand Down
47 changes: 41 additions & 6 deletions src/JsonLogTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public partial class JsonLogTextBox : UserControl

public BorderStyle LogBorderStyle { get; set; } = BorderStyle.Fixed3D;

public string ForContext { get; set; } = string.Empty;

private bool _isContextConfigured = false;

public JsonLogTextBox()
{
InitializeComponent();
Expand All @@ -30,24 +34,55 @@ private void LogTextBox_Load(object sender, EventArgs e)
WindFormsSink.JsonTextBoxSink.OnLogReceived += JsonTextBoxSinkOnLogReceived;
}

private void JsonTextBoxSinkOnLogReceived(string str)
private void JsonTextBoxSinkOnLogReceived(string context, string str)
{
if (_isContextConfigured)
{
if (!string.IsNullOrEmpty(this.ForContext)
&& !string.IsNullOrEmpty(context)
&& this.ForContext.Equals(context, StringComparison.InvariantCultureIgnoreCase)) { PrintText(str); }
}
else
{
PrintText(str);
}

Application.DoEvents();
}

private void PrintText(string str)
{
if (this.InvokeRequired)
{
this.Invoke(
(MethodInvoker)delegate
{
TxtLogControl.AppendText(str);
TxtLogControl.ScrollToCaret();
});
{
TxtLogControl.AppendText(str);
TxtLogControl.ScrollToCaret();
});
}
else
{
TxtLogControl.AppendText(str);
TxtLogControl.ScrollToCaret();
}
}

Application.DoEvents();
public void ClearLogs()
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)(() => TxtLogControl.Clear()));
}
else
{
TxtLogControl.Clear();
}
}

public void SaveLogToFile()
{
SaveFileHelper.SaveLogsToFile(TxtLogControl.Text);
}
}
}
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.2.6.0")]
[assembly: AssemblyFileVersion("2.2.6.0")]
[assembly: AssemblyVersion("2.3.0.0")]
[assembly: AssemblyFileVersion("2.3.0.0")]
[assembly: NeutralResourcesLanguage("en")]
40 changes: 37 additions & 3 deletions src/RichTextBoxLogControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,36 @@ namespace Serilog.Sinks.WinForms
{
public partial class RichTextBoxLogControl : RichTextBox
{
public string ForContext { get; set; } = string.Empty;
private bool _isContextConfigured = false;

public RichTextBoxLogControl()
{
InitializeComponent();
WindFormsSink.SimpleTextBoxSink.OnLogReceived += SimpleTextBoxSinkOnLogReceived;
}

private void SimpleTextBoxSinkOnLogReceived(string str)
private void SimpleTextBoxSinkOnLogReceived(string context, string str)
{
if (_isContextConfigured)
{
if (!string.IsNullOrEmpty(this.ForContext)
&& !string.IsNullOrEmpty(context)
&& this.ForContext.Equals(context, StringComparison.InvariantCultureIgnoreCase)) { PrintText(str); }
}
else
{
PrintText(str);
}

Application.DoEvents();
}

private void PrintText(string str)
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
this.Invoke((MethodInvoker) delegate
{
this.AppendText(str);
this.ScrollToCaret();
Expand All @@ -26,8 +45,23 @@ private void SimpleTextBoxSinkOnLogReceived(string str)
this.AppendText(str);
this.ScrollToCaret();
}
}

Application.DoEvents();
public void ClearLogs()
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)(() => this.Clear()));
}
else
{
this.Clear();
}
}

public void SaveLogToFile()
{
SaveFileHelper.SaveLogsToFile(this.Text);
}
}
}
2 changes: 1 addition & 1 deletion src/Sample/CustomSimpleTextLog/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void Form1_Load(object sender, EventArgs e)
WindFormsSink.SimpleTextBoxSink.OnLogReceived += SimpleTextBoxSinkOnOnLogReceived;
}

private void SimpleTextBoxSinkOnOnLogReceived(string str)
private void SimpleTextBoxSinkOnOnLogReceived(string context, string str)
{
this.textBox1.AppendText(str);
this.richTextBox1.AppendText(str);
Expand Down
43 changes: 42 additions & 1 deletion src/Sample/TestApplication/Form2.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/Sample/TestApplication/Form2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace TestApplication
{
public partial class Form2 : Form
{
private static readonly ILogger _logger = Log.ForContext<Form2>();

public Form2()
{
InitializeComponent();
Expand All @@ -22,7 +24,7 @@ public Form2()

private void button1_Click(object sender, EventArgs e)
{
Log.Information(textBox1.Text);
_logger.Information(textBox1.Text);
}

private void button2_Click(object sender, EventArgs e)
Expand All @@ -33,8 +35,18 @@ private void button2_Click(object sender, EventArgs e)
}
catch (Exception exception)
{
Log.Error(exception, "Error Happened in Form2");
_logger.Error(exception, "Error Happened in Form2");
}
}

private void button3_Click(object sender, EventArgs e)
{
simpleLogTextBox1.ClearLogs();
}

private void button4_Click(object sender, EventArgs e)
{
simpleLogTextBox1.SaveLogToFile();
}
}
}
6 changes: 4 additions & 2 deletions src/Sample/TestApplication/LogVieweer.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Sample/TestApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static void Main()
private static void ConfigureSerilog()
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteToGridView()
.WriteToJsonTextBox()
.WriteToSimpleAndRichTextBox()
Expand Down
26 changes: 26 additions & 0 deletions src/SaveFileHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.IO;
using System.Windows.Forms;

namespace Serilog.Sinks.WinForms
{
public static class SaveFileHelper
{
internal static void SaveLogsToFile(string fileContent)
{
try
{
var saveFileDialog = new SaveFileDialog { Filter = @"Text Files | *.txt| Log Files |*.log" };

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
File.AppendAllText(saveFileDialog.FileName, fileContent);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, @"Error Saving Log File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
1 change: 1 addition & 0 deletions src/Serilog.Sinks.WinForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="JsonLogTextBox.Designer.cs">
<DependentUpon>JsonLogTextBox.cs</DependentUpon>
</Compile>
<Compile Include="SaveFileHelper.cs" />
<Compile Include="SimpleLogTextBox.cs">
<SubType>UserControl</SubType>
</Compile>
Expand Down
Loading

0 comments on commit f46bbe3

Please sign in to comment.