Skip to content

Commit f46bbe3

Browse files
committed
Log for context and save and clear log options implemented
1 parent 96ab78f commit f46bbe3

14 files changed

+287
-43
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ Log.Logger = new LoggerConfiguration()
6060
.CreateLogger();
6161
```
6262

63+
#### Log For Context
64+
65+
SimpleLogTextBox, RichTextBoxLogControl or JsonLogTextBox can be configured to display log for specific ***Context***.
66+
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)
67+
68+
**Note:** Remember to configure Logger instance with ***Enrich.FromLogContext()***
69+
70+
71+
#### Save and Clear Logs
72+
SimpleLogTextBox, RichTextBoxLogControl or JsonLogTextBox have two method available for saving log to file or clear the log from the log control.
73+
***ClearLogs()*** AND ***SaveLogToFile()***
74+
75+
Check the usage in sample application here [Sample](https://github.com/umairsyed613/Serilog.Sinks.WinForms/blob/master/src/Sample/TestApplication/Form2.cs)
76+
77+
6378
### Sample Code
6479

6580
Find the sample running application [here](https://github.com/umairsyed613/Serilog.Sinks.WinForms/tree/master/Sample/TestApplication/)

src/JsonLogTextBox.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public partial class JsonLogTextBox : UserControl
1313

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

16+
public string ForContext { get; set; } = string.Empty;
17+
18+
private bool _isContextConfigured = false;
19+
1620
public JsonLogTextBox()
1721
{
1822
InitializeComponent();
@@ -30,24 +34,55 @@ private void LogTextBox_Load(object sender, EventArgs e)
3034
WindFormsSink.JsonTextBoxSink.OnLogReceived += JsonTextBoxSinkOnLogReceived;
3135
}
3236

33-
private void JsonTextBoxSinkOnLogReceived(string str)
37+
private void JsonTextBoxSinkOnLogReceived(string context, string str)
38+
{
39+
if (_isContextConfigured)
40+
{
41+
if (!string.IsNullOrEmpty(this.ForContext)
42+
&& !string.IsNullOrEmpty(context)
43+
&& this.ForContext.Equals(context, StringComparison.InvariantCultureIgnoreCase)) { PrintText(str); }
44+
}
45+
else
46+
{
47+
PrintText(str);
48+
}
49+
50+
Application.DoEvents();
51+
}
52+
53+
private void PrintText(string str)
3454
{
3555
if (this.InvokeRequired)
3656
{
3757
this.Invoke(
3858
(MethodInvoker)delegate
39-
{
40-
TxtLogControl.AppendText(str);
41-
TxtLogControl.ScrollToCaret();
42-
});
59+
{
60+
TxtLogControl.AppendText(str);
61+
TxtLogControl.ScrollToCaret();
62+
});
4363
}
4464
else
4565
{
4666
TxtLogControl.AppendText(str);
4767
TxtLogControl.ScrollToCaret();
4868
}
69+
}
4970

50-
Application.DoEvents();
71+
public void ClearLogs()
72+
{
73+
if (this.InvokeRequired)
74+
{
75+
this.Invoke((MethodInvoker)(() => TxtLogControl.Clear()));
76+
}
77+
else
78+
{
79+
TxtLogControl.Clear();
80+
}
81+
}
82+
83+
public void SaveLogToFile()
84+
{
85+
SaveFileHelper.SaveLogsToFile(TxtLogControl.Text);
5186
}
5287
}
5388
}

src/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("2.2.6.0")]
37-
[assembly: AssemblyFileVersion("2.2.6.0")]
36+
[assembly: AssemblyVersion("2.3.0.0")]
37+
[assembly: AssemblyFileVersion("2.3.0.0")]
3838
[assembly: NeutralResourcesLanguage("en")]

src/RichTextBoxLogControl.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,36 @@ namespace Serilog.Sinks.WinForms
55
{
66
public partial class RichTextBoxLogControl : RichTextBox
77
{
8+
public string ForContext { get; set; } = string.Empty;
9+
private bool _isContextConfigured = false;
10+
811
public RichTextBoxLogControl()
912
{
1013
InitializeComponent();
1114
WindFormsSink.SimpleTextBoxSink.OnLogReceived += SimpleTextBoxSinkOnLogReceived;
1215
}
1316

14-
private void SimpleTextBoxSinkOnLogReceived(string str)
17+
private void SimpleTextBoxSinkOnLogReceived(string context, string str)
18+
{
19+
if (_isContextConfigured)
20+
{
21+
if (!string.IsNullOrEmpty(this.ForContext)
22+
&& !string.IsNullOrEmpty(context)
23+
&& this.ForContext.Equals(context, StringComparison.InvariantCultureIgnoreCase)) { PrintText(str); }
24+
}
25+
else
26+
{
27+
PrintText(str);
28+
}
29+
30+
Application.DoEvents();
31+
}
32+
33+
private void PrintText(string str)
1534
{
1635
if (this.InvokeRequired)
1736
{
18-
this.Invoke((MethodInvoker)delegate
37+
this.Invoke((MethodInvoker) delegate
1938
{
2039
this.AppendText(str);
2140
this.ScrollToCaret();
@@ -26,8 +45,23 @@ private void SimpleTextBoxSinkOnLogReceived(string str)
2645
this.AppendText(str);
2746
this.ScrollToCaret();
2847
}
48+
}
2949

30-
Application.DoEvents();
50+
public void ClearLogs()
51+
{
52+
if (this.InvokeRequired)
53+
{
54+
this.Invoke((MethodInvoker)(() => this.Clear()));
55+
}
56+
else
57+
{
58+
this.Clear();
59+
}
60+
}
61+
62+
public void SaveLogToFile()
63+
{
64+
SaveFileHelper.SaveLogsToFile(this.Text);
3165
}
3266
}
3367
}

src/Sample/CustomSimpleTextLog/Form1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private void Form1_Load(object sender, EventArgs e)
2424
WindFormsSink.SimpleTextBoxSink.OnLogReceived += SimpleTextBoxSinkOnOnLogReceived;
2525
}
2626

27-
private void SimpleTextBoxSinkOnOnLogReceived(string str)
27+
private void SimpleTextBoxSinkOnOnLogReceived(string context, string str)
2828
{
2929
this.textBox1.AppendText(str);
3030
this.richTextBox1.AppendText(str);

src/Sample/TestApplication/Form2.Designer.cs

Lines changed: 42 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Sample/TestApplication/Form2.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace TestApplication
1414
{
1515
public partial class Form2 : Form
1616
{
17+
private static readonly ILogger _logger = Log.ForContext<Form2>();
18+
1719
public Form2()
1820
{
1921
InitializeComponent();
@@ -22,7 +24,7 @@ public Form2()
2224

2325
private void button1_Click(object sender, EventArgs e)
2426
{
25-
Log.Information(textBox1.Text);
27+
_logger.Information(textBox1.Text);
2628
}
2729

2830
private void button2_Click(object sender, EventArgs e)
@@ -33,8 +35,18 @@ private void button2_Click(object sender, EventArgs e)
3335
}
3436
catch (Exception exception)
3537
{
36-
Log.Error(exception, "Error Happened in Form2");
38+
_logger.Error(exception, "Error Happened in Form2");
3739
}
3840
}
41+
42+
private void button3_Click(object sender, EventArgs e)
43+
{
44+
simpleLogTextBox1.ClearLogs();
45+
}
46+
47+
private void button4_Click(object sender, EventArgs e)
48+
{
49+
simpleLogTextBox1.SaveLogToFile();
50+
}
3951
}
4052
}

src/Sample/TestApplication/LogVieweer.Designer.cs

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Sample/TestApplication/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static void Main()
2323
private static void ConfigureSerilog()
2424
{
2525
Log.Logger = new LoggerConfiguration()
26+
.Enrich.FromLogContext()
2627
.WriteToGridView()
2728
.WriteToJsonTextBox()
2829
.WriteToSimpleAndRichTextBox()

src/SaveFileHelper.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.IO;
3+
using System.Windows.Forms;
4+
5+
namespace Serilog.Sinks.WinForms
6+
{
7+
public static class SaveFileHelper
8+
{
9+
internal static void SaveLogsToFile(string fileContent)
10+
{
11+
try
12+
{
13+
var saveFileDialog = new SaveFileDialog { Filter = @"Text Files | *.txt| Log Files |*.log" };
14+
15+
if (saveFileDialog.ShowDialog() == DialogResult.OK)
16+
{
17+
File.AppendAllText(saveFileDialog.FileName, fileContent);
18+
}
19+
}
20+
catch (Exception e)
21+
{
22+
MessageBox.Show(e.Message, @"Error Saving Log File", MessageBoxButtons.OK, MessageBoxIcon.Error);
23+
}
24+
}
25+
}
26+
}

src/Serilog.Sinks.WinForms.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="JsonLogTextBox.Designer.cs">
6767
<DependentUpon>JsonLogTextBox.cs</DependentUpon>
6868
</Compile>
69+
<Compile Include="SaveFileHelper.cs" />
6970
<Compile Include="SimpleLogTextBox.cs">
7071
<SubType>UserControl</SubType>
7172
</Compile>

0 commit comments

Comments
 (0)