Hello @Darren Rose
The following are code snippets from this class project in this repository on GitHub converted from this C# cide in this repository on GitHub.
Notes
- There is more code than simply shown here
- The conversion was not straightforward as I used C# 9 features that have no equivalent in VB which took a tad longer than expected.
Hopefully this is of use to you and note what you were looking at is for use with web apps.
.NET Core version
https://github.com/karenpayneoregon/csharp-features/tree/master/ConsoleApp_vb
Program.vb
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.Extensions.Logging
Module Program
Sub Main(args As String())
Dim serviceCollection As New ServiceCollection()
ConfigureServices(serviceCollection)
Dim serviceProvider = serviceCollection.BuildServiceProvider()
Dim someClass = serviceProvider.GetService(Of SomeClass)()
someClass.LogInfo("Hello")
End Sub
Private Sub ConfigureServices(ByVal services As IServiceCollection)
services.AddLogging(Function(configure) configure.AddConsole()).AddTransient(Of SomeClass)()
End Sub
End Module
Supporting class
Imports Microsoft.Extensions.Logging
Public Class SomeClass
Private ReadOnly _logger As ILogger(Of SomeClass)
Private Sub New(logger As ILogger(Of SomeClass))
_logger = logger
End Sub
Private Sub LogCritical(message As String)
_logger.Log(LogLevel.Critical, message)
_logger.LogInformation("")
End Sub
Public Sub LogInfo(message As String)
_logger.LogInformation(message)
End Sub
End Class
Enum
Public Enum LogLevel
Warning
Errors
General
End Enum
Interface
Public Interface ILogger
Property FileName() As String
Sub LogWarning(message As String)
Sub LogError(message As String)
Sub LogGeneral(message As String)
Sub Log(ByVal level As LogLevel, ByVal message As String)
End Interface
Write to a file
Public Class LogManager
Implements ILogger
Public Property FileName() As String Implements ILogger.FileName
Public Sub LogWarning(message As String) Implements ILogger.LogWarning
Log(LogLevel.Warning, message)
End Sub
Public Sub LogError(message As String) Implements ILogger.LogError
Log(LogLevel.Errors, message)
End Sub
Public Sub LogGeneral(message As String) Implements ILogger.LogGeneral
Log(LogLevel.General, message)
End Sub
Public Sub Log(ByVal level As LogLevel, ByVal message As String) Implements ILogger.Log
If String.IsNullOrWhiteSpace(FileName) Then
FileName = "ApplicationLog.csv"
End If
Try
If File.Exists(FileName) Then
Dim contents = File.ReadAllLines(FileName).ToList()
contents.Add($"{DateTime.Now},{level},{message}")
File.WriteAllLines(FileName, contents.ToArray())
Else
File.WriteAllText(FileName, $"{DateTime.Now},{level},{message}")
End If
Catch e As Exception
Exceptions.Write(e)
End Try
End Sub
End Class
Write to the console
Public Class ConsoleLogger
Implements ILogger
Public Property FileName As String Implements ILogger.FileName
Public Sub LogWarning(message As String) Implements ILogger.LogWarning
Log(LogLevel.Warning, message)
End Sub
Public Sub LogError(message As String) Implements ILogger.LogError
Log(LogLevel.Errors, message)
End Sub
Public Sub LogGeneral(message As String) Implements ILogger.LogGeneral
Log(LogLevel.General, message)
End Sub
Public Sub Log(level As LogLevel, message As String) Implements ILogger.Log
Console.WriteLine($"{level}, {message}")
End Sub
End Class
AddLogging is not a member of IServiceCollection
Make sure you have the NuGet package Microsoft.Extensions.DependencyInjection installed and the others I've listed below which is accessed by double clicking the project file in VS2019 or if a lower version of VS, upload the project and edit the project file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<RootNamespace>WinFormsAppILogger</RootNamespace>
<StartupObject>Sub Main</StartupObject>
<UseWindowsForms>true</UseWindowsForms>
<MyType>WindowsForms</MyType>
</PropertyGroup>
<ItemGroup>
<Import Include="System.Data" />
<Import Include="System.Drawing" />
<Import Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="My Project\Application.Designer.vb">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Application.myapp</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="My Project\Application.myapp">
<Generator>MyApplicationCodeGenerator</Generator>
<LastGenOutput>Application.Designer.vb</LastGenOutput>
</None>
</ItemGroup>
</Project>