How do you use System.Diagnostics.TextWriterTraceListener to log to a file in C#? Are my configuration files broken?

bp256r1

I'm new to C# and am trying to add the ability to log messages to a file - I've been trying to figure it out for a few hours, but, I'm not very good at this.

I don't know very much about assemblies, solutions, App.config, .csproj, etc., and all of this extremely basic information is way over my head.

Here's my Program.cs:

using System.Diagnostics;

namespace Main;

class Program
{
  static void Main(string[] args)
  {
    try
    {
      Trace.AutoFlush = true;
      Trace.TraceInformation("Application started.");
      Trace.WriteLine("Hello World!");
      Trace.Flush();
      Trace.Close();
    }
    catch (Exception ex)
    {
      Console.WriteLine("Exception: " + ex.Message);
    }
  }
}

Whenever I run the program with dotnet run, the log file isn't created.

If I compile the project with dotnet publish, the log file still isn't created, and I have no idea why.

Here's my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="logListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Users\tyler\src\windows-audit-monitor\trace.log" />
        <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

Here's my app.csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <RootNamespace>Main</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <Configuration>Release</Configuration>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.3" />
    <PackageReference Include="NetMQ" Version="4.0.1.12" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>

</Project>

Here's my app.manifest:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

I have no idea which very, very, very basic step I screwed up, and know that this is all very shameful.

I already have some other code that's working, but maybe it'd make sense for me to start over with a tutorial so I can make sure I'm using the right build toolchain and configuration files...

I mainly work in Python, Go, Java, Ruby, bash, and PowerShell but am really, really, really bad at C#....

mabene

I'd consider going with the Microsoft.Extensions.Logging framework when writing code for .net7, possibly using an extension for logging to file:

Add a Package reference for a logging provider that supports many useful options for logging to files <PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />

Add using statement to the top of your code

using Serilog;

Create a logger and write sample message:

Log.Logger = new LoggerConfiguration()
  .MinimumLevel.Information()
  .WriteTo.File(@"c:\log\LoggingSample.log")
  .CreateLogger();
Log.Logger.Information("Hello world");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you use the Write System Call in C++?

How often do you need to set the configuration file for log4j2?

How do you use RedisSessionHandler in Symfony 4? Broken docs

How do you configure the log files written to rel/myproject/log?

How do you use input redirection from a file in C?

How do you use java files in Coldfusion

How do you use ImageMagick script files?

How do you use regex to parse text across multiple lines from a log file?

my project can't use System.Diagnostics.Process

How do I 'use diagnostics' for Perl on Cygwin?

Logging in C++: How do you produce separate log files for different objects and their children?

How do I use sed to change my configuration files, with flexible keys and values?

How do you log to an Android device like system apps?

How do you use a font from your file system HTML CSS

How do you manage embedded configuration files and libraries in java webapps?

how do you include variables in Bind9 configuration files?

How do you modify this script to run TinyPNG on opened files instead of having to use the open dialog to select a file?

How to change the way the Linux File System is interpreting my file? My source code file is read as a 'broken link', despite it still a text file

How do you use an identity file with rsync?

How do you create and use a .cma file?

How do you use StreamWriter to write to a file

How do you use posix_spawn to replace the deprecated 'system' to launch opendiff in Objective-C?

How do I get permissions to edit system configuration files?

How do you mock out the file system in C# for unit testing?

How do you use Spring Retry with Spring Vault Configuration with VaultPropertySource?

How do you keep only the last n lines of a log file?

How do I fix this type of error System.Diagnostics.DebuggerStepThrough()

How do you use QAbstractOpenGLFunctions in c++?

How do you use a vector in c++?