我是C#的新手,正在try 添加将消息记录到文件中的功能--我已经try 了几个小时,但我对此并不是很在行.

我对程序集、解决方案、App.config、.csproj等不太了解,所有这些非常基本的信息都超出了我的理解范围.

以下是我的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);
    }
  }
}

每当我使用dotnet run运行程序时,都不会创建日志(log)文件.

如果我用dotnet publish编译项目,日志(log)文件仍然没有创建,我不知道为什么.

以下是我的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>

这是我的app.csproj%文件:

<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>

以下是我的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>

我不知道我搞砸了哪一个非常、非常、非常基本的步骤,我知道这一切都是非常可耻的.

我已经有一些正在运行的其他代码,但对我来说,从教程开始可能更有意义,这样我就可以确保使用正确的构建工具链和配置文件……

我主要在Python、Go、Java、Ruby、bash和PowerShell中工作,但我的C#非常、非常、非常糟糕.

推荐答案

在为.net7编写代码时,我会考虑使用Microsoft.Extensions.Logging框架,可能会使用扩展名来记录到文件:

为支持用于记录到文件的许多有用选项的日志(log)记录提供程序添加包引用 <PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />

将Using语句添加到代码顶部

using Serilog;

创建一个记录器并编写示例消息:

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

Csharp相关问答推荐

将.NET 8程序集加载到Matlab R2023a中时出现问题

C#中的多yield 机制

C#中的两个线程之间读写浮点类型

使用变量子根名称在C#中重新初始化SON文件

获取ASP.NET核心身份认证cookie名称

处理. netstandard2.0项目中HttpClient.SslProtocol的PlatformNotSupportedException问题""

并行令牌更新

Entity Framework Core 8 dbcontext—无法以多对多关系添加某些行'

在C#WinUI中,一个关于System的崩溃."由于未知原因导致执行不例外"

在实时数据库中匹配两个玩家的问题

为什么SignalR在每个Blazor服务器应用程序启动时最多启动8个服务器?

在路由中使用枚举

StackExchange.Redis.RedisServerException:调用ITransaction.ExecuteAsync()时出现错误未知命令取消监视

用C#从Word文档中删除重复的节控件和文本内容控件

按需无缝转码单个HLS数据段

如何在mediatr命令中访问HttpContext而不安装弃用的nuget包

Foreach非常慢的C#

Azure函数正在返回值列表,但该列表在Chrome中显示为空

从HTML元素获取 colored颜色

为什么C#中的类型别名不能在另一个别名中使用?