如何检测,如果我在控制台快到 [英] How do I detect if I'm running in the console

查看:101
本文介绍了如何检测,如果我在控制台快到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的办法有一个code库,如果它正在从一个控制台应用程序或Windows应用程序中调用自动检测?我想我的库不报告到Windows事件日志中,如果它正在从一个控制台窗口调用,而是报告给控制台窗口。但是,如果它没有被在控制台窗口中运行,它应该报告给Windows事件日志。

Is there a simple way to have a code library automatically detect if it's being called from a console application or a windows application? I'd like my library not to report to the Windows Event log if it's being called from a console window, but instead report to the console window. If however, it's not being run from within a console window, it should report to the Windows Event Log.

我想到了我的要求的记录组件要传递的日志目标,但它是整齐的,如果它可能只是自动在本地处理这两个目标。我还不需要的东西尽可能全面,log4net的,事实上,如果有必要提供支持登录到一个数据库/文件和其他目前未知的日志目标,那么我可能会建议这样的解决方案。尽管如此,现在只是有我的部分根据环境将会有大量的自动检测环境,登录到控制台或事件日志。

I thought about requiring my the logging component to be passed the log target, but it would be neat if it could just automatically handle these two targets natively. I don't yet require something as extensive as log4net, indeed, if it becomes necessary to provide support to log to a database/file and other as yet unknown logging targets, then I may recommend such a solution. For now though, just having my component auto-detect the environment and log to the console or the event log according to the environment would be plenty.

推荐答案

在结构上,通过日志记录上下文连接到库组件是正确的选择。图书馆没有,确实不应该,知道有关它正在运行的环境中很多方面。

Architecturally, passing the logging context into the library component is the right choice. The library doesn't, and indeed shouldn't, know that much context about the environment it's being run in.

由于要在库中的原生支持这两种特殊情况,我建议一个统一的方法。

Because you want to support these two special cases natively within the library, I'd suggest a unified approach.

  1. 在继续前进,创造更广义的记录切入点/旋钮,来电者控制。
  2. 创建一个单独的入口点/旋钮自动设置广义之一要自动支持的情况下。

即使这样似乎太根据您的描述复杂的,虽然。你是否考虑过简单地使用你的诊断收集,在您的控制台应用程序添加适当的TraceListener输出到控制台和非控制台应用程序添加适当的事件日志的TraceListener输出到Windows事件日志中适当TraceListeners?这与运行良好的附加优势全内置的.NET日志支持而不承担任何外部的依赖关系(如log4net的)。

Even that seems too complicated based on your description, though. Have you considered simply using appropriate TraceListeners in your Diagnostics collection, where your console app adds the appropriate TraceListener to output to the console and the non-console app adds the appropriate EventLog TraceListener to output to the Windows event log? This has the added advantage of working well with all the built-in .net logging support without assuming any external dependencies (e.g., log4net).

这篇关于如何检测,如果我在控制台快到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆