请教高手,如何用vb实现“使用日志”?

来自:    更新日期:早些时候
如何用VB实现日志功能~

说得具体些。

一楼写得有点麻烦
我给你个简单点的吧!需要一个时间控件和一个标签
时间控件的enabled 设为true
interva设为1000 也就是1秒执行一次这样就能动态现实
2008奥运会开始时间应该是8月15吧!

Private Sub Timer1_Timer()
Dim time As Long
Dim d, h, m, s As Integer
time = DateDiff("s", Now, #8/14/2008 12:00:00 PM#)
d = Int(time / 86400)
h = Int((time - d * 86400) / 3600)
m = Int((time - d * 86400 - h * 3600) / 60)
s = (time - d * 86400 - h * 3600 - m * 60)
L1.Caption = d & "天" & h & "小时" & m & "分" & s & "秒"
End Sub

很多种方法,最常用的一是用文本文件或数据库记录,二是调用API函数直接写入系统日志或应用程序日志

.NET Framework 类库
EventLogInstaller 类
使您能够安装和配置应用程序在运行时所读取或写入的事件日志。

命名空间:System.Diagnostics
程序集:System.Configuration.Install(在 system.configuration.install.dll 中)

语法

Visual Basic(声明)Public Class EventLogInstaller
Inherits ComponentInstaller

Visual Basic(用法)Dim instance As EventLogInstaller

C#public class EventLogInstaller : ComponentInstaller

C++public ref class EventLogInstaller : public ComponentInstaller

J#public class EventLogInstaller extends ComponentInstaller

JScriptpublic class EventLogInstaller extends ComponentInstaller

备注

EventLogInstaller 由 安装程序工具 (Installutil.exe) 在安装事件日志时使用。EventLogInstaller 类只能在本地计算机上安装事件日志。

应用程序写入事件日志时使用 EventLogInstaller 类;应用程序要读取事件日志中的内容,不一定要使用事件日志安装程序。应用程序和服务应写入 Application 日志或自定义日志。设备驱动程序应写入 System 日志。

注意
Security 日志是只读的。

安装程序创建您在 Source 属性中指定的事件源,并为 Log 属性中指定的事件日志注册该事件源。此行为类似于对 EventLog 组件调用 CreateEventSource。

使用 WriteEvent 和 WriteEntry 方法可以向事件日志中写入事件。若要写入事件,必须指定一个事件源;在事件源中写入第一项之前,必须先创建并配置事件源。

在安装应用程序的过程中创建新的事件源。这样操作系统就有时间刷新自己的已注册事件源的列表以及这些事件源的配置。如果操作系统尚未刷新自己的事件源列表,而您试图在新源中写入事件,则写操作将会失败。您可以使用 EventLogInstaller 或者使用 CreateEventSource 方法配置新的源。若要创建新的事件源,您必须拥有计算机的管理员权限。

可以为现有事件日志或新的事件日志创建事件源。在为新的事件日志创建新源时,系统会为该日志注册源,但直到写入第一条日志时才会创建该日志。

若要安装事件日志,请创建一个从 Installer 继承的项目安装程序类,并将该类的 RunInstallerAttribute 设置为 true。在项目中,为应用程序将写入的每一个事件日志创建一个 EventLogInstaller,并将该实例添加到项目安装程序类中。

调用 安装程序工具 (Installutil.exe) 时,它将查看 RunInstallerAttribute。如果该属性为 true,此工具将安装 Installers 集合中与项目安装程序关联的所有项。如果 RunInstallerAttribute 为 false,此工具将忽略项目安装程序。

若要修改 EventLogInstaller 的其他属性,可以在将该实例添加到项目安装程序的 Installers 集合之前或之后(但必须在安装程序工具运行之前)执行。如果您的应用程序将写入事件日志,则必须设置 Source 属性。

使用 EventLogInstaller 为新的或现有的事件日志注册一个新的源;不要使用 EventLogInstaller 更改现有的源。EventLogInstaller 类不会为了与指定的安装属性匹配而修改现有源的配置属性。如果 Source 属性与某个源名称匹配,而该源名称是为计算机上的另一个事件日志注册的,则 Install 方法会引发异常。如果 Source 属性与某个源名称匹配,而该源名称已经为 Log 属性中指定的同一事件日志注册,则 Install 方法将不会注册对应的源。

可以使用事件类别和消息字符串的本地化资源文件来注册事件源。您的应用程序可使用资源标识符而不是通过指定实际字符串来写入事件日志项。事件查看器使用资源标识符,根据当前的语言设置从本地化资源文件中查找对应的字符串,并将其显示出来。您可以为事件类别、消息和参数插入字符串注册单独的文件,或者,也可以为所有这三种类型的字符串注册同一个资源文件。使用 CategoryCount、CategoryResourceFile、MessageResourceFile 和 ParameterResourceFile 属性配置源以将本地化项写入事件日志。如果您的应用程序直接将字符串值写入事件日志,则不需要设置这些属性。

无论要写入本地化后的项还是要直接写入字符串,都必须配置源。如果应用程序写入项时既使用资源标识符也使用字符串值,则必须注册两个不同的源。例如,用资源文件配置一个源,然后在 WriteEvent 方法中使用该源,将使用资源标识符的项写入到事件日志。在不使用资源文件的情况下另外创建一个源,然后在 WriteEntry 方法中使用该源将字符串直接写入到使用该源的事件日志。

通常不在代码中调用 EventLogInstaller 类的方法,这些方法一般仅由安装程序工具 Installutil.exe 调用。该工具在安装过程中自动调用 Install 方法。它在必要时对生成异常的对象调用 Rollback 以退出故障。

Windows 98, Windows Millennium Edition 平台说明: Windows 98/Windows Millennium Edition (Me) 不支持事件日志。

示例

下面的代码示例为一个新的事件源设置安装属性。此代码示例设置源名称和事件日志名称,并将 EventLogInstaller 添加到 Installers 集合中。

Visual Basic 复制代码Imports System
Imports System.Configuration.Install
Imports System.Diagnostics
Imports System.ComponentModel

<RunInstaller(True)> _
Public Class MyEventLogInstaller
Inherits Installer
Private myEventLogInstaller As EventLogInstaller

Public Sub New()

' Create an instance of an EventLogInstaller.
myEventLogInstaller = New EventLogInstaller()

' Set the source name of the event log.
myEventLogInstaller.Source = "NewLogSource"

' Set the event log that the source writes entries to.
myEventLogInstaller.Log = "MyNewLog"

' Add myEventLogInstaller to the Installer collection.
Installers.Add(myEventLogInstaller)
End Sub 'New

Public Shared Sub Main()
End Sub 'Main
End Class 'MyEventLogInstaller

C# 复制代码using System;
using System.Configuration.Install;
using System.Diagnostics;
using System.ComponentModel;

[RunInstaller(true)]
public class MyEventLogInstaller: Installer
{
private EventLogInstaller myEventLogInstaller;

public MyEventLogInstaller()
{
// Create an instance of an EventLogInstaller.
myEventLogInstaller = new EventLogInstaller();

// Set the source name of the event log.
myEventLogInstaller.Source = "NewLogSource";

// Set the event log that the source writes entries to.
myEventLogInstaller.Log = "MyNewLog";

// Add myEventLogInstaller to the Installer collection.
Installers.Add(myEventLogInstaller);
}

public static void Main()
{
}
}

C++ 复制代码#using <System.dll>
#using <System.Configuration.Install.dll>

using namespace System;
using namespace System::Configuration::Install;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

[RunInstaller(true)]
ref class MyEventLogInstaller: public Installer
{
private:
EventLogInstaller^ myEventLogInstaller;

public:
MyEventLogInstaller()
{
// Create an instance of an EventLogInstaller.
myEventLogInstaller = gcnew EventLogInstaller;

// Set the source name of the event log.
myEventLogInstaller->Source = "NewLogSource";

// Set the event log that the source writes entries to.
myEventLogInstaller->Log = "MyNewLog";

// Add myEventLogInstaller to the Installer collection.
Installers->Add( myEventLogInstaller );
}
};

J# 复制代码import System.*;
import System.Configuration.Install.*;
import System.Diagnostics.*;
import System.ComponentModel.*;

/** @attribute RunInstaller(true)
*/
public class MyEventLogInstaller extends Installer
{
private EventLogInstaller myEventLogInstaller;

public MyEventLogInstaller()
{
// Create an instance of an EventLogInstaller.
myEventLogInstaller = new EventLogInstaller();

// Set the source name of the event log.
myEventLogInstaller.set_Source("NewLogSource");

// Set the event log that the source writes entries to.
myEventLogInstaller.set_Log("MyNewLog");

// Add myEventLogInstaller to the Installer collection.
this.get_Installers().Add(myEventLogInstaller);
} //MyEventLogInstaller

public static void main(String[] args)
{
} //main
} //MyEventLogInstaller

继承层次结构

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Configuration.Install.Installer
System.Configuration.Install.ComponentInstaller
System.Diagnostics.EventLogInstaller

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

版本信息

.NET Framework
受以下版本支持:2.0、1.1、1.0

请参见

参考
EventLogInstaller 成员
System.Diagnostics 命名空间
EventLog 类
CreateEventSource

其他资源
安装程序工具 (Installutil.exe)

有这么麻烦吗?用数据库记录不就得了,容易的不得了.


请教高手,如何用vb实现“使用日志”?视频

相关评论:
  • 19529152568vb调用键盘输入
    霍翟都应该是:sendkeys"(^%{DOWN})"把按键放到圆括号内表示同时按下,功能健要放在大括号内!

  • 19529152568请教VB高手,急!急!急!
    霍翟都Dim a As String, b As Integer b不应该为Integer,会导致程序出错 If a = "abcd " And b = "12345" Then abcd后面多了个空格 而且应该用elseif 来连接 号码输入的验证 再次补充一下 :a = Text1.Text b = Text2.Text 这样的话会提示类型不匹配 '---顺便发一下Integer 数据类型--- In...

  • 19529152568想用VB模拟鼠标移动和点击操作,请教高手
    霍翟都SetCursorPos 吧鼠标移到这个点上 然后用 mouse_event 模拟点击

  • 19529152568用VB如何解方程式
    霍翟都你这个方程至少应该整理成ax+b=c的形式吧 你这个是一元一次方程很简单,是一条直线,用这个性质去解 先定义两个变量,设第一个变量为x1=0;如果a*x1+b<c表示这个解比x1大,然后定义第二个变量x2=100(随便假设)。再算。如果a*x2+b>c表示这个解在x1和x2之间。再取其中点在运算,根据实际...

  • 19529152568VB菜鸟 请教高手关于Asc()函数
    霍翟都then msgbox "输入错误,最后缺少%号":Exit sub if num1=百分号位置 and 百分号位置=len(CNC代码)then msgbox "输入错误,开头缺少%号":Exit sub === 这个~~不会吧~~那~你用英文的变量名试试~~

  • 19529152568在EXCEL中用VB编写程序时,如何调用EXCEL中某个表格的数据?
    霍翟都如果你插入的是窗体控件,很简单的。(activex控件比较麻烦,得用vba代码)右键点车间(1)控件,设置格式,最后一个标签“控制”,单元格链接处点到比如sheet1的g1,确定。在sheet2的a1输入 =if(sheet1!g1,"车间(1)","车间(2)")就可以了。

  • 19529152568如何学习使用EXCEL中的VBA功能?
    霍翟都3. 可以使用EXCEL自带的“记录宏”功能,作为对入门的学习。4. 要用好VBA,要多学习一些关于表格的基础知识,比例什么是工作薄,什么是工作表,什么是单元格。再关注一些关于SQL的知识,会让你的表格使用更上一层楼。5. 买本关于VB的书籍比如(Excel 2007与VBA编程从入门到精通),学习一些编程的基础...

  • 19529152568vb.net 中我现实现,线程的暂停与开始!请教高手一下!请留下你的联系方 ...
    霍翟都在你给textbox设置text值后直接使用sleep(),因为在使用sleep()的时候,当前的主进程是被挂起了,也就是说设置的text属性后,还来不及在屏幕上显示lbi的内容就执行了sleep,所以在执行sleep()挂起主进程前,你必须让textbox的内容先显示出来再执行sleep,可尝试使用下DoEvents()将控制权暂时移交一下...

  • 19529152568VB中如何获得date型数据的时、分、秒?分别是多少?请教高手~_百度...
    霍翟都利用VB自带的函数 hour(date) ‘ 小时m = minute(date) ' 分钟 s = second(date) ' 秒

  • 19529152568请教高手:VB编程:将1到10这十个数,按每个记录(每个记录为1行)2个数 ...
    霍翟都Private Sub Command1_Click()Open "data1.txt" For Output As #1 For i = 1 To 10 Step 2 Print #1, i, i + 1 Next End Sub

  • 相关主题精彩

    版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

    Copyright © 喜物网