当应用程序作为 XBAP 运行时记录日志?

发布于 2024-08-10 14:54:17 字数 287 浏览 6 评论 0原文

当应用程序作为 XBAP 运行时,这里有人实际上实施过任何日志记录策略吗?关于如何根据您的经验实施简单策略的任何建议(作为代码)。

我的桌面模式应用程序实际上使用集成的 asop log4net 实现记录到日志文件(滚动日志),但在 xbap 中我无法记录,因为它将文件存储在缓存(app2.0 或其他文件夹)中,所以我检查浏览器是否托管和不要记录,因为我什至不知道它是否记录过...(为什么相同的代码库)...如果有一种方法可以将此日志推送到 Web 服务等服务或将错误发布到某个端点...

我的 xbap是完全信任内网模式。

Anybody here has actually implemented any logging strategy when application is running as XBAP ? Any suggestion (as code) as to how to implement a simple strategy base on your experience.

My app in desktop mode actually logs to a log file (rolling log) using integrated asop log4net implementation but in xbap I can't log cause it stores the file in cache (app2.0 or something folder) so I check if browser hosted and dont log since i dont even know if it ever logs...(why same codebase)....if there was a way to push this log to a service like a web service or post error to some endpoint...

My xbap is full trust intranet mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半葬歌 2024-08-17 14:54:17

我将记录到独立的存储,并为用户提供一种使用简单的 PUT/POST 和 HttpWebRequest 或通过 WCF 服务将日志提交回服务器的方法。

请记住,XBAP 仅获得 512k 的独立存储,因此您实际上可能希望自动将这些事件日志推送回服务器。另请记住,XBAP 只能与其原始服务器进行交互,因此接受日志文件的服务必须在同一域下运行。

下面是一些快速示例代码,展示了如何设置 TextWriterTraceListener 位于 IsolatedStorageFileStream 此时您可以只使用标准的Trace.Write[XXX] 方法 来进行日志记录。

IsolatedStorageFileStream traceFileStream = new IsolatedStorageFileStream("Trace.log", FileMode.OpenOrCreate, FileAccess.Write);

TraceListener traceListener = new TextWriterTraceListener(traceFileStream);

Trace.Listeners.Add(traceListener);

更新

这是由于您对问题进行了修改并提供了更多详细信息而修改的答案。

既然您提到您在桌面应用程序中使用 log4net,我们就可以基于您已经习惯使用的依赖关系进行构建,因为完全有可能继续在 XBAP 版本中使用 log4net。 Log4net 没有提供可以立即解决此问题的实现,但可以编写 log4net 的实现 IAppender 与 WCF 通信。

我查看了 Joachim Kerschbaumer 的其他回答者链接到的实现(所有功劳归于)并且它看起来像是一个可靠的实现。我首先担心的是,在示例中,有人可能会在每个事件上登录回服务,并且可能是同步的,但该实现实际上支持对一定数量的事件进行排队并以批处理形式将它们发送回服务器。此外,当它确实发送到服务时,它会使用 异步调用一个 Action 委托,这意味着它将在线程池线程上执行并且不会阻塞 UI。因此我想说,实施是相当扎实的。

以下是我将在此处执行的步骤:

  1. 下载 Joachim 的 WCF 附加程序实现
  2. 将他的项目添加到您的项目中解决方案。
  3. 从 XBAP
  4. 配置 log4net 引用 WCFAppender 项目以使用 WCF Appender。现在,这个记录器有几个设置,所以我建议检查他的示例应用程序的配置。然而,最重要的是 QueueSize 和 FlushLevel。您应该将 QueueSize 设置得足够高,以便根据您实际记录的数量,您不会与 WCF 服务进行过多的交互。如果您只是配置警告/错误,那么您可以将其设置为较低的值。如果您使用信息进行配置,那么您需要将其设置得更高一些。就 FlushLevel 而言,您可能应该将其设置为 ERROR,因为这将保证无论错误发生时队列有多大,所有内容都将在记录错误时刷新。
  5. 该示例似乎使用 LINQ2SQL 记录到 WCF 服务内部的自定义数据库。您将需要替换此实现以记录到最适合您需求的任何数据源。

现在,Joachim 的示例以一种易于下载、运行和快速理解的方式编写。如果我将其放入生产解决方案中,我肯定会更改一些内容:

  1. 将 WCF 合约分离到一个单独的库中,您可以在客户端和服务器之间共享该库。这将允许您停止使用 WCFAppender 库中的 Visual Studio 服务引用,而仅引用数据类型的相同约定库。同样,由于合同不再位于服务本身中,因此您可以从服务中引用合同库。
  2. 我不知道 wsHttpBinding 这里真的有必要。它配备了比像这样简单的东西可能需要的更多的旋钮和开关。我可能会选择更简单的 basicHttpBinding,如果您想确保日志数据通过网络加密,我只需确保使用 HTTPS。

I would log to isolated storage and provide a way for users to submit the log back to the server using either a simple PUT/POST with HttpWebRequest or, if you're feeling frisky, via a WCF service.

Keep in mind an XBAP only gets 512k of isolated storage so you may actually want to push those event logs back to the server automatically. Also remember that the XBAP can only speak back to it's origin server, so the service that accepts the log files must run under the same domain.

Here's some quick sample code that shows how to setup a TextWriterTraceListener on top of an IsolatedStorageFileStream at which point you can can just use the standard Trace.Write[XXX] methods to do your logging.

IsolatedStorageFileStream traceFileStream = new IsolatedStorageFileStream("Trace.log", FileMode.OpenOrCreate, FileAccess.Write);

TraceListener traceListener = new TextWriterTraceListener(traceFileStream);

Trace.Listeners.Add(traceListener);

UPDATE

Here is a revised answer due to the revision you've made to your question with more details.

Since you mention you're using log4net in your desktop app we can build upon that dependency you are already comfortable working with as it is entirely possible to continue to use log4net in the XBAP version as well. Log4net does not come with an implementation that will solve this problem out of the box, but it is possible to write an implementation of a log4net IAppender which communicates with WCF.

I took a look at the implementation the other answerer linked to by Joachim Kerschbaumer (all credit due) and it looks like a solid implementation. My first concern was that, in a sample, someone might be logging back to the service on every event and perhaps synchronously, but the implementation actually has support for queuing up a certain number of events and sending them back to the server in batch form. Also, when it does send to the service, it does so using an async invocation of an Action delegate which means it will execute on a thread pool thread and not block the UI. Therefore I would say that implementation is quite solid.

Here's the steps I would take from here:

  1. Download Joachim's WCF appender implementation
  2. Add his project's to your solution.
  3. Reference the WCFAppender project from your XBAP
  4. Configure log4net to use the WCF appender. Now, there are several settings for this logger so I suggest checking out his sample app's config. The most important ones however are QueueSize and FlushLevel. You should set QueueSize high enough so that, based on how much you actually are logging, you won't be chattering with the WCF service too much. If you're just configuring warnings/errors then you can probably set this to something low. If you're configuring with informational then you want to set this a little higher. As far as FlushLevel you should probably just set this to ERROR as this will just guarantee that no matter how big the queue is at the time an error occurs everything will be flushed at the moment an error is logged.
  5. The sample appears to use LINQ2SQL to log to a custom DB inside of the WCF service. You will need to replace this implementation to log to whatever data source best suits your needs.

Now, Joachim's sample is written in a way that's intended to be very easy for someone to download, run and understand very quickly. I would definitely change a couple things about it if I were putting it into a production solution:

  1. Separate the WCF contracts into a separate library which you can share between the client and the server. This would allow you to stop using a Visual Studio service reference in the WCFAppender library and just reference the same contract library for the data types. Likewise, since the contracts would no longer be in the service itself, you would reference the contract library from the service.
  2. I don't know that wsHttpBinding is really necessary here. It comes with a couple more knobs and switches than one probably needs for something as simple as this. I would probably go with the simpler basicHttpBinding and if you wanted to make sure the log data was encrypted over the wire I would just make sure to use HTTPS.
泪是无色的血 2024-08-17 14:54:17

我的方法是登录到远程服务,由唯一的用户 ID 或 GUID 键入。通常的异步调用的开销并不是很高。

您也可以在本地缓存消息,无论是在 RAM 中还是在独立存储中——也许作为网络无法访问时的备份。

请务必注意特定时间范围内的重复事件。您不希望在几秒钟内记录同一异常的 1,000 个副本。

另外,我喜欢记录的不仅仅是错误。您还可以记录性能数据,例如某些函数执行所需的时间(特别是进程外调用),或者响应用户显式进入“调试和报告”模式的更详细数据。检查花费时间超过特定阈值的呼叫也有助于捕获回归并预防用户投诉。

My approach has been to log to a remote service, keyed by a unique user ID or GUID. The overhead isn't very high with the usual async calls.

You can cache messages locally, too, either in RAM or in isolated storage -- perhaps as a backup in case the network isn't accessible.

Be sure to watch for duplicate events within a certain time window. You don't want to log 1,000 copies of the same Exception over a period of a few seconds.

Also, I like to log more than just errors. You can also log performance data, such as how long certain functions take to execute (particularly out-of-process calls), or more detailed data in response to the user explicitly entering into a "debug and report" mode. Checking for calls that take longer than a certain threshold is also useful to help catch regressions and preempt user complaints.

挖鼻大婶 2024-08-17 14:54:17

如果您在部分信任下运行 XBAP,则仅允许您写入客户端计算机上的isolatedStorage。它只有 512 KB,您可能希望以更有价值的方式使用它(比用于日志记录),例如存储用户的首选项。

在部分信任下,您也不允许执行任何 Remoting 操作,因此您无法使用 log4net RemotingAppender。

最后,在部分信任 XBAP 下,您拥有 WebPermission 与应用程序来源的服务器对话仅。我建议使用 WCF 服务, 如本文所述。我们在我当前的项目中使用类似的配置,并且效果很好。

然后,基本上,在 WCF 服务器端,您可以将日志记录到任何适当的位置:文件、数据库等。您可能还想保留 log4net 日志记录代码并尝试使用互联网上可用的 wcf 日志附加程序之一(< a href="http://blog.joachim.at/?p=31" rel="nofollow noreferrer">此 或 这个)。

If you are running your XBAP under partial trust, you are only allowed to write to the IsolatedStorage on the client machine. And it's just 512 KB, which you would probably want to use in a more valuable way (than for logging), like for storing user's preferences.

You are not allowed to do any Remoting stuff as well under partial trust, so you can't use log4net RemotingAppender.

Finally, under partial trust XBAP you have WebPermission to talk to the server of your app origin only. I would recommend using a WCF service, like described in this article. We use similar configuration in my current project and it works fine.

Then, basically, on the WCF server side you can do logging to any place appropriate: file, database, etc. You may also want to keep your log4net logging code and try to use one of the wcf log appenders available on the internets (this or this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文