我不知道为什么,但许多计算机在执行以下操作时挂起:
void Init()
{
net1 = new List<PerformanceCounter>();
net2 = new List<PerformanceCounter>();
foreach (string instance in new PerformanceCounterCategory("Network Interface").GetInstanceNames())
{
net1.Add(new PerformanceCounter("Network Interface", "Bytes Received/sec", instance));
net2.Add(new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance));
}
}
//Once in 1 second
void UpdateStats()
{
Status.Text = "";
for (int i = 0; i < net1.Count; i++)
Status.Text += string.Format("{0}/{1} Kb/sec; ", net1[i].NextValue() / 1024, net2[i].NextValue() / 1024);
}
Run Code Online (Sandbox Code Playgroud)
在某些计算上,程序在第一次调用 时完全挂起UpdateStats(),而其他计算则经历 100% CPU 负载,但程序运行(缓慢)。其他计数器似乎new PerformanceCounter("Processor", "% Processor Time", "_Total")工作正常。
有什么建议吗?这是为什么?
执行以下代码时,searcher.Get()抛出异常“不支持”。即使以管理员身份运行,问题仍然存在。这是用VS 2010编译的。在XP,Win7和Win8.1上会发生异常。有任何想法吗??
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
}
Run Code Online (Sandbox Code Playgroud) 我在让 Azure 应用程序日志记录与我的 Web 应用程序配合使用时遇到问题。我可以成功发布并使用我的网络应用程序。然而,当我查看日志时,“应用程序”文件夹是空的。我可以成功地在 stdout.log 文件中看到我写入控制台的消息...但应用程序文件夹中从来没有任何内容。
至于我所尝试的,我已按照此处概述的步骤进行操作。看起来很简单。配置 Azure 以打开应用程序日志记录,在代码中编写跟踪命令,并且内容应写入日志中的应用程序文件夹。我还尝试将应用程序日志记录 (Blob) 设置为打开,但我也没有看到其中写入任何内容。
下面是我在 Azure 中配置的屏幕截图:
这是我用代码写入 Trace 的示例:
public class HomeController : Controller
{
public IActionResult Index()
{
Trace.TraceInformation("At Home");
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
这是使用 ASP.NET 5/MVC 6。请注意,我也尝试过使用 TraceWarning 和 TraceError ,但没有成功。我还尝试打开 Azure 中的所有日志记录选项,但这似乎也没有任何作用。
c# system.diagnostics azure asp.net-core-mvc azure-web-app-service
首先,我是一个幼稚的开发者,对这些代码没有太深入的理解。我在尝试获取应用程序之一的版本号的代码块中访问被拒绝。
场景:在 Visual Studio 中运行代码时没有问题。但是,当安装文件在不同的计算机上运行并检查日志时,它会抛出Win32Exception:访问被拒绝。
(程序在安装后立即自动运行。实际上,在安装此应用程序之前,已经安装了一个看门狗服务,该服务会自动运行该应用程序并在关闭时恢复它。所以,我相信我无法将requestedExecutionLevel设置为requireAdmin,它可能会显示确认对话框并给用户对运行应用程序的控制。)
我需要解决这个问题。在阅读了一些博客后,我认为这一定是由于缺乏足够的权限来检索所需信息。但我仍然不清楚如何解决/解决这个问题。
日志文件中的异常:
[ 1,11216] 2017-04-17T11:43:53 - -------------------- Win32Exception caught --------------------
[ 1,11216] 2017-04-17T11:43:53 - Access is denied
[ 1,11216] 2017-04-17T11:43:53 - (Win32Err=0x00000005)
[ 1,11216] 2017-04-17T11:43:53 - Stack trace is :
[ 1,11216] 2017-04-17T11:43:53 - at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at IMPMDesktopClient.BaseIMClientDriver.GetVersion(UIntPtr windowUIntPtr)
at IMPMDesktopClient.WindowDetector.Hooks.WindowsEventArgs..ctor(Int32 eventNum, UIntPtr windowHandle, Int32 idObject, Int32 idChild, String clsName, Rectangle pos)
at IMPMDesktopClient.WindowDetector.Hooks.EventHooks.CheckForWindowOfInterest(UIntPtr hWnd, …Run Code Online (Sandbox Code Playgroud) 在我的网络应用程序中,我使用
System.Diagnostics.Trace.WriteLine(...);
Run Code Online (Sandbox Code Playgroud)
用于诊断。应用程序发布到 Azure 部署槽。我想知道在 Azure 门户(或 Visual Studio 的云资源管理器)中哪里可以找到这些日志。
c# system.diagnostics azure azure-application-insights visual-studio-2017
我尝试使用System.Diagnostics.Process命名空间在C#程序中调用chrome.exe。
我的chrome.exe位于路径C:\Program Files (x86)\Google\Chrome\Application内
如果我通过传递以下参数来调用RunProc函数 - (保留 exe 的绝对路径并保持 WalkingDirectory 为空)
(“C:\ Program Files(x86)\ Google \ Chrome \ Application \ Chrome.exe”,“ https://www.google.com ”,“”)它工作得很好。
但是,有了参数 -
( "Chrome.exe , " https://www.google.com ", "C:\Program Files (x86)\Google\Chrome\Application" ) 它在步骤 proc.Start(); 中给出异常;说明 -系统找不到指定的文件。
我还尝试在初始化 StartInfo 时编写WorkingDirectory =workingDir,但仍在寻找解决方案。
class Program
{
static void Main(string[] args)
{
RunProc(@"chrome.exe", @"https://www.google.com", @"C:\Program Files (x86)\Google\Chrome\Application");
}
static bool RunProc(string exe, string args, …Run Code Online (Sandbox Code Playgroud) 我有一堂课:
public abstract class InvestorAccount
{
public bool IsRegistered => ...;
}
Run Code Online (Sandbox Code Playgroud)
还有一个子类:
public class PrivateInvestorAccount : InvestorAccount
{
public Person? Holder { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想教编译器,当is时,它Holder不是 null 。IsRegisteredtrue
如果两个 props 都在同一个类中声明,则可以仅IsRegistered使用进行注释[MemberNotNullWhen(true, nameof(Holder))]。然而,在继承场景下,应用
[MemberNotNullWhen(true, nameof(PrivateInvestorAccount.Holder))]最终会出现编译错误:
错误CS8776:成员“持有者”不能在此属性中使用。
有什么办法告诉编译器Holder不属于null这种情况吗?
配置文件:
<system.diagnostics>
<trace>
<listeners>
<add name="Console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false"></add>
<add name="Text" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\uat\logs\bifit.log" traceOutputOptions="DateTime"></add>
</listeners>
</trace>
</system.diagnostics>
Run Code Online (Sandbox Code Playgroud)
代码:
static void Main(string[] args)
{
try
{
Trace.WriteLine("Running BiFit Test Server.");
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在cosole窗口看到文字.并创建文件,但它是空的.我记得某处有Autoflush属性吗?
我有一些代码可以创建一个Process实例,然后启动它.有一些逻辑需要检查Process是否已经启动.HasExited可用于检查是否已退出已启动的进程,但我找不到类似的功能HasStarted.乍一看StartTime看起来是一个不错的选择,但是如果进程退出,这个函数将抛出.此外,文档说StartTime只对启动的进程有意义.
确定流程是否已启动(已启动但可能已退出)的"正确"方法是什么?
我在命令行中运行此应用程序并获得所需的结果
Helpdesk-02.exe /department it
Run Code Online (Sandbox Code Playgroud)
但是我的C#代码(下面)似乎忽略了参数,但是在没有命令行开关的情况下启动应用程序
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"Y:\Helpdesk-02.exe";
psi.Arguments = @"/department it";
psi.UseShellExecute = true;
Process.Start(psi).WaitForExit();
Run Code Online (Sandbox Code Playgroud)