2

I have to implement equivalent code of Visual Basic "App.StartLogging" and "App.LogEvent" in .NET C#. App.StartLogging has the two parameters i.e. logTarget and logMode.
(https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-basic-6/aa267165(v=vs.60))

I tried to find out same type of logging event in C#, but unfortunately not able to get. System. On some website, found like System.Diagnostics.EventLog is the alternate of App.StartLogging. But not getting how ? App.StartLogging help to log data in local file, but i didn't get same behavior in System.Diagnostics.EventLog.

Can you please help me to solve my issue ?

  • Maybe check out Log4Net. With the ~same name, you can find a paid version and a open source version (`Apache log4net`). – Jimi Feb 17 '22 at 06:23
  • As @Jimi suggested, you should look into 3rd party .NET logging libraries such as the mentioned Log4Net. I personally use [NLog](https://www.nuget.org/packages/NLog/5.0.0-rc2), but that's just my personal preference. Both will do the required job and more. – Hel O'Ween Feb 17 '22 at 08:16
  • @JohnEason The application that is now migrating(?) to C# (or VB.Net?) is written in Visual Basic. The OP is asking how to *migrate* `App.StartLogging` etc. – Jimi Feb 17 '22 at 10:41
  • 1
    Oops. Mea culpa! I'd never come across those logging keywords in VB6 before. :^( – John Eason Feb 17 '22 at 11:12
  • Here's one I wrote and you can replace the File logging with Log4Net or NLog or even using the System.Diagnostics.EventLog https://stackoverflow.com/questions/30326673/user-activity-logging-telemetry-and-variables-in-global-exception-handlers – Jeremy Thompson Feb 18 '22 at 03:54
  • See `ReportEvent` at https://learn.microsoft.com/en-us/windows/win32/eventlog/event-logging-functions – KL-1 Feb 18 '22 at 07:01
  • `CreateObject("WScript.Shell").LogEvent 1, "Error"` – KL-1 Feb 18 '22 at 20:21

1 Answers1

0

There is no direct replacement, see https://www.vbmigration.com/resources/detmigratingfromvb6.aspx?Id=19,

A few App members are used to log application events - namely the LogMode and LogPath properties and the StartLogging and LogEvent methods. These members have no direct counterparts in the .NET Framework.

You will other have to write your equivalents, or pick a 3rd party logging tool that can do what you need.

For instance, nLog will allow you to log to both the file system and to the Event Viewer. Logging an event is simple you simply write to the log, the StartLogging event is going to be harder, I don’t know of any logging framework or library that creates an event for that, typically it starts before everything else.

jmoreno
  • 12,752
  • 4
  • 60
  • 91