public class App {
/// <summary> The Logger for this plugin. </summary>
/// <remarks> Cannot use the Static <see cref="Serilog.Log"/> because each plugin needs its own <see cref="Logger"/>
/// <para /> https://stackoverflow.com/questions/46900488/serilog-logger-for-each-plugin-dll-in-one-application </remarks>
internal static Logger Log;
// ....
public void Initialize()
{
string logFileName = Environment.GetFolderPath
(Environment.SpecialFolder.ApplicationData) + "\\CADbloke\\CADtools\\Logs\\CADfindReplace-{Date}.log";
Assembly assembly
= Assembly
.GetAssembly(typeof(App
)); // <== "App" is the class name here, use your class name FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
Log
= new LoggerConfiguration
()#if DEBUG
.MinimumLevel.Verbose()
#endif
.Enrich.WithProperty("Version", version)
.WriteTo.RollingFile(logFileName, outputTemplate: "[v{Version}] {Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}")
.CreateLogger();
// ...
}
// when you use it ....
App.Log.Information("CAD find Replace woke up");
// ...
catch (Exception ex)
{
App.Log.Error
(ex,
messageTemplate: "Failed setting Implied Selection. Dwg: {Dwg}, Refedit Block Name: {Refedit}",
propertyValue0: filename,
propertyValue1: refEditBlockName()); // I don't usually put the parameter names in, that's to clarify how Serilog works
...
}
// when you are closing the app
public void Terminate()
{
App.Log.Dispose(); // Flushes the logger
// ...
}