Skip to content

Commit e7aca09

Browse files
Add sample WinForms project hosting the WPF RichTextBox control
1 parent f62c3ca commit e7aca09

File tree

6 files changed

+569
-1
lines changed

6 files changed

+569
-1
lines changed

sample/WinFormsHostNet50Sample/MainForm.Designer.cs

Lines changed: 204 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Globalization;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Forms;
9+
using System.Windows.Forms.Integration;
10+
using System.Windows.Media;
11+
using Serilog;
12+
using Serilog.Debugging;
13+
14+
namespace WinFormsHostNet50Sample
15+
{
16+
public partial class MainForm : Form
17+
{
18+
private static readonly object _syncRoot = new object();
19+
private readonly System.Windows.Controls.RichTextBox _wpfRichTextBox;
20+
21+
public MainForm()
22+
{
23+
InitializeComponent();
24+
25+
var richTextBoxHost = new ElementHost
26+
{
27+
Dock = DockStyle.Fill,
28+
};
29+
30+
_richTextBoxPanel.Controls.Add(richTextBoxHost);
31+
32+
var wpfRichTextBox = new System.Windows.Controls.RichTextBox
33+
{
34+
Background = Brushes.Black,
35+
Foreground = Brushes.LightGray,
36+
FontFamily = new FontFamily("Cascadia Mono, Consolas, Courier New, monospace"),
37+
FontSize = 14,
38+
IsReadOnly = true,
39+
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
40+
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
41+
Margin = new Thickness(0),
42+
};
43+
44+
richTextBoxHost.Child = wpfRichTextBox;
45+
_wpfRichTextBox = wpfRichTextBox;
46+
47+
_clearToolStripButton.Click += Clear_OnClick;
48+
_logVerboseToolStripButton.Click += LogVerbose_OnClick;
49+
_logDebugToolStripButton.Click += LogDebug_OnClick;
50+
_logInformationToolStripButton.Click += LogInformation_OnClick;
51+
_logWarningToolStripButton.Click += LogWarning_OnClick;
52+
_logErrorToolStripButton.Click += LogError_OnClick;
53+
_logFatalToolStripButton.Click += LogFatal_OnClick;
54+
_logParallelForToolStripButton.Click += LogParallelFor_OnClick;
55+
_logTaskRunToolStripButton.Click += LogTaskRun_OnClick;
56+
57+
SelfLog.Enable(message => Trace.WriteLine($"INTERNAL ERROR: {message}"));
58+
59+
const string outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}";
60+
61+
Log.Logger = new LoggerConfiguration()
62+
.MinimumLevel.Verbose()
63+
.WriteTo.RichTextBox(wpfRichTextBox, outputTemplate: outputTemplate, syncRoot: _syncRoot)
64+
.Enrich.WithThreadId()
65+
.CreateLogger();
66+
}
67+
68+
private void MainForm_Load(object sender, EventArgs e)
69+
{
70+
Log.Debug("Getting started");
71+
72+
Log.Information("Hello {Name} from thread {ThreadId}", Environment.UserName,
73+
Thread.CurrentThread.ManagedThreadId);
74+
75+
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
76+
77+
try
78+
{
79+
Fail();
80+
}
81+
catch (Exception ex)
82+
{
83+
Log.Error(ex, "Oops... Something went wrong");
84+
}
85+
}
86+
87+
private static void Fail()
88+
{
89+
throw new DivideByZeroException();
90+
}
91+
92+
private void Clear_OnClick(object sender, EventArgs e)
93+
{
94+
lock (_syncRoot)
95+
{
96+
_wpfRichTextBox.Document.Blocks.Clear();
97+
}
98+
}
99+
100+
private void LogVerbose_OnClick(object sender, EventArgs e)
101+
{
102+
Log.Verbose("Hello! Now => {Now}", DateTime.Now);
103+
}
104+
105+
private void LogDebug_OnClick(object sender, EventArgs e)
106+
{
107+
Log.Debug("Hello! Now => {Now}", DateTime.Now);
108+
}
109+
110+
private void LogInformation_OnClick(object sender, EventArgs e)
111+
{
112+
Log.Information("Hello! Now => {Now}", DateTime.Now);
113+
}
114+
115+
private void LogWarning_OnClick(object sender, EventArgs e)
116+
{
117+
Log.Warning("Hello! Now => {Now}", DateTime.Now);
118+
}
119+
120+
private void LogError_OnClick(object sender, EventArgs e)
121+
{
122+
Log.Error("Hello! Now => {Now}", DateTime.Now);
123+
}
124+
125+
private void LogFatal_OnClick(object sender, EventArgs e)
126+
{
127+
Log.Fatal("Hello! Now => {Now}", DateTime.Now);
128+
}
129+
130+
private void LogParallelFor_OnClick(object sender, EventArgs e)
131+
{
132+
Parallel.For(1, 101, stepNumber =>
133+
{
134+
var stepName = $"Step {stepNumber.ToString("000", CultureInfo.InvariantCulture)}";
135+
136+
Log.Verbose("Hello from Parallel.For({StepName}) Verbose", stepName);
137+
Log.Debug("Hello from Parallel.For({StepName}) Debug", stepName);
138+
Log.Information("Hello from Parallel.For({StepName}) Information", stepName);
139+
Log.Warning("Hello from Parallel.For({StepName}) Warning", stepName);
140+
Log.Error("Hello from Parallel.For({StepName}) Error", stepName);
141+
Log.Fatal("Hello from Parallel.For({StepName}) Fatal", stepName);
142+
});
143+
}
144+
145+
private async void LogTaskRun_OnClick(object sender, EventArgs e)
146+
{
147+
var tasks = new System.Collections.Generic.List<Task>();
148+
149+
for (var i = 1; i <= 100; i++)
150+
{
151+
var stepNumber = i;
152+
var task = Task.Run(() =>
153+
{
154+
var stepName = $"Step {stepNumber.ToString("000", CultureInfo.InvariantCulture)}";
155+
156+
Log.Verbose("Hello from Task.Run({StepName}) Verbose", stepName);
157+
Log.Debug("Hello from Task.Run({StepName}) Debug", stepName);
158+
Log.Information("Hello from Task.Run({StepName}) Information", stepName);
159+
Log.Warning("Hello from Task.Run({StepName}) Warning", stepName);
160+
Log.Error("Hello from Task.Run({StepName}) Error", stepName);
161+
Log.Fatal("Hello from Task.Run({StepName}) Fatal", stepName);
162+
});
163+
164+
tasks.Add(task);
165+
}
166+
167+
await Task.WhenAll(tasks);
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)