Skip to content

Latest commit

 

History

History
64 lines (61 loc) · 3.83 KB

README.md

File metadata and controls

64 lines (61 loc) · 3.83 KB

WFAppLogger

WinForm Application Logger example

C# WinForm Application example that uses Microsoft.Extensions.Logging for logging. This demonstrates a uniform logging methodology when using Entity Framework Core or other .NET Core libraries in a WinForm Application. This example uses both ConsoleLogger and FileLogger, and uses Microsoft.Extensions.DependencyInjection for creating Logger and Form classes.

Most of the online example code available relied on settings in a separate appsettings.json file or only provided file logging through a secondary framework extension such as NLog or SeriLog. In this example, the logging settings utilize standard Microsoft WinForm application settings as XML in the App.config file (below) and the lightweight FileLogger.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="WFAppLogger.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
    <!-- some sections omitted for brevity ... !-->
  <userSettings>
    <WFAppLogger.Properties.Settings>
      <setting name="DefaultLogMessage" serializeAs="String">
        <value>Sample log message</value>
      </setting>
      <setting name="LoggingConfig" serializeAs="Xml">
        <value>
          <Root>
            <Logging>
              <LogLevel>
                <Default>Debug</Default>
                <WFAppLogger>Trace</WFAppLogger>
                <Microsoft>Warning</Microsoft>
                <System>Warning</System>
              </LogLevel>
              <Console>
                <IncludeScopes>true</IncludeScopes>
              </Console>
              <File>
                <IncludeScopes>true</IncludeScopes>
                <!-- Log files will be written to %TEMP%[\%BasePath%]\<appname>-<startdate>-<counter>.log -->
                <!--<BasePath>Logs</BasePath>-->
                <Files>
                  <File>
                    <Path>&lt;appname&gt;-&lt;startdate:yyyyMMdd-HHmmss&gt;-&lt;counter:000&gt;.log</Path>
                    <MaxFileSize>100000</MaxFileSize>
                  </File>
                </Files>
              </File>
            </Logging>
          </Root>
        </value>
      </setting>
    </WFAppLogger.Properties.Settings>
  </userSettings>
</configuration>

The example also provides custom log file Path settings:

Path Template Parameter Notes
appname Application Name (e.g. WFAppLogger)
startdate The DateTime that file logging was started. It can include a standard .NET format string to be passed to DateTime.ToString (e.g. "<appname>-<startdate:yyyyMMdd-HHmmss>-<counter:000>.log" will create a log file such as "WFAppLogger-20200410-105108-001.log")