打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# 生成事件log

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Threading;

using System.IO;

namespace WindowsFormsApp31

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

        }

        private string str = "";

        private void Form1_Load(object sender, EventArgs e)

        {

            Thread th1 = new Thread(test1);

            th1.Start();

            Thread th2 = new Thread(test2);

            th2.Start();

            timer1.Enabled = true;

        }

        public void test1()

        {

            try

            {

                if (!Directory.Exists(@"d:\test"))

                    Directory.CreateDirectory(@"d:\test");

                if (!File.Exists(@"d:\test\1.txt"))

                    File.Create(@"d:\test\1.txt").Close();

                StreamWriter sw = new StreamWriter(@"d:\test\1.txt", true);

                for (int i = 0; i < 100; i++)

                {

                    sw.WriteLine(i.ToString());

                }

                sw.Close();

                int k = 0;

                k = k / 0;//引发异常,写入事件log

            }

            catch(Exception ex)

            {

                StreamWriter sw = new StreamWriter(@"d:\test\1.txt", true);

                sw.WriteLine(ex.ToString());

                sw.Close();

            }

        }

        public void test2()

        {

            try

            {

                if (!Directory.Exists(@"d:\test"))

                    Directory.CreateDirectory(@"d:\test");

                if (!File.Exists(@"d:\test\2.txt"))

                    File.Create(@"d:\test\2.txt").Close();

                StreamWriter sw = new StreamWriter(@"d:\test\2.txt", true);

                for (int i = 0; i < 100; i++)

                {

                    sw.WriteLine(i.ToString());

                }

                sw.Close();

            }

            catch(Exception ex)

            {

                StreamWriter sw = new StreamWriter(@"d:\test\2.txt", true);

                sw.WriteLine(ex.ToString());

                sw.Close();

            }

        }

        private void timer1_Tick(object sender, EventArgs e)

        {

            str += "1";

            richTextBox1.Text = str;

        }

    }

}

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

System.DivideByZeroException: 尝试除以零。

   在 WindowsFormsApp31.Form1.test1() 位置 C:\Users\lin\source\repos\WindowsFormsApp31\WindowsFormsApp31\Form1.cs:行号 58

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#读写txt文件的两种方法介绍
.NET写入文本文件的操作浅析 - 51CTO.COM
C#写入和读出文本文件
C#学习笔记(22)
DataGridView中的数据导入Excel .
获取数据库表结构和表数据的小程序(VB.NET版本)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服