我是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#非常、非常、非常糟糕.