设为首页 收藏本站
查看: 1475|回复: 0

[经验分享] IIS日志导入Sql数据库

[复制链接]

尚未签到

发表于 2015-8-14 10:55:20 | 显示全部楼层 |阅读模式
  1. 建立表结构
  SET ANSI_NULLS ON
GO
  SET QUOTED_IDENTIFIER ON
GO
  SET ANSI_PADDING ON
GO
  CREATE TABLE [dbo].[web_201007](
[date] [varchar](255) NULL,
[time] [varchar](255) NULL,
[s-ip] [varchar](255) NULL,
[cs-method] [varchar](255) NULL,
[cs-uri-stem] [nvarchar](255) NULL,
[cs-uri-query] [nvarchar](1000) NULL,
[s-port] [varchar](255) NULL,
[cs-username] [varchar](255) NULL,
[c-ip] [varchar](255) NULL,
[cs(User-Agent)] [nvarchar](1000) NULL,
[cs(Referer)] [nvarchar](1000) NULL,
[sc-status] [varchar](255) NULL,
[sc-substatus] [varchar](255) NULL,
[sc-win32-status] [varchar](255) NULL,
[time-taken] [varchar](255) NULL
) ON [PRIMARY]
  GO
  SET ANSI_PADDING OFF
GO
  注:a. 由于数据量比较大,所以分表保存,上面表名是7月份的。
       b. 表的结构要根据你日志保存的字段来创建哦。
  2. 使用bcp导入数据
  bcp [database].dbo.[web_201007] in "g:\iislog\log.txt" -w -t"*" -r"\r" -U"sa" -P"123456" -S"UKEASSERVER\SQLEXPRESS"
  -t:列分隔符
  -r:行分隔符
  -w:使用unicode编码。如果你日志里包含有中文的用-w,如果没有,用-c就好了,-w会使你的数据库文件变很大的。
  log.txt是日志文件,如果有中文,要保存为unicode编码,不要用utf8啦,不然bcp的时候都是乱码了,就是说bcp不支持utf8的。没有中文的话,ANSI就ok了。
  注:出现乱码很恶心的,经常说你的表结构有问题啊,什么字段不够长啊,数据字符串被截断了啊。
  
  我的IIS日志文件都是*.log文件,而且每天一个文件,不是txt文件啊,怎么办呢?
  自己写代码啊,把每个月的数据文件读出来,去掉没有用的行,再保存到log.txt文件里就好了,这个处理过程比bcp时间快多了。
  

DSC0000.gif DSC0001.gif IIS日志转换

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.IO;
  5 using System.Text.RegularExpressions;
  6
  7 namespace Log2Sql
  8 {
  9     public class FormatLog
10     {
11         private string inputDirectory;
12         private string outputFileName;
13         private string errorFileName;
14         private string fieldSeparator = "*";
15         private string rowSeparator = "\r";
16         private int fieldLength = 15;
17
18         public FormatLog(string inputDirectory, string outputFileName, string errorFileName)
19         {
20             this.inputDirectory = inputDirectory;
21             this.outputFileName = outputFileName;
22             this.errorFileName = errorFileName;
23         }
24
25         public void Execute()
26         {
27             Regex reg = new Regex("u_ex(\\d{6})\\.log");
28             string[] files = Directory.GetFiles(inputDirectory);
29             List<string> temp = new List<string>();
30             foreach(string f in files){
31                 Match match = reg.Match(f);
32                 string date = match.Groups[1].Value;
33                 if(date.StartsWith("1011")){
34                     temp.Add(f);
35                 }
36             }
37             files = temp.ToArray();
38             using(FileStream fs = new FileStream(outputFileName, FileMode.Create)) {
39                 using(StreamWriter sw = new StreamWriter(fs, Encoding.Unicode)) {
40                     foreach(string f in files) {
41                         Save(GetRecordLineForFile(f), sw);
42                     }
43                 }
44             }
45         }
46
47         public void WriteInvalidFields()
48         {
49             using(FileStream fs = new FileStream(errorFileName, FileMode.Create)){
50                 using(StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)){
51                     using(FileStream fs2 = new FileStream(outputFileName, FileMode.Open)) {
52                         using(StreamReader sr = new StreamReader(fs2, Encoding.UTF8)) {
53                             string lineData = sr.ReadLine();
54                             while(!string.IsNullOrEmpty(lineData)) {
55                                 List<string> fields = GetInvalidFields(lineData);
56                                 foreach(string f in fields) {
57                                     sw.WriteLine(string.Format("{0} {1}", f.Length, f));
58                                 }
59                                 lineData = sr.ReadLine();
60                             }
61                         }
62                     }
63                 }
64             }
65         }
66
67         private void Save(List<string> datas, StreamWriter sw)
68         {
69             foreach(string s in datas) {
70                 sw.Write(s);
71                 sw.Write(rowSeparator);
72             }
73         }
74
75         private List<string> GetRecordLineForFile(string fileName)
76         {
77             List<string> list = new List<string>();
78             try{
79                 using(FileStream fs = new FileStream(fileName, FileMode.Open)){
80                     using(StreamReader sr = new StreamReader(fs, Encoding.UTF8)){
81                         string lineData = sr.ReadLine();
82                         while(!string.IsNullOrEmpty(lineData)) {
83                             if(!lineData.StartsWith("#")) {
84                                 lineData = lineData.Replace(" ", fieldSeparator);
85                                 if(lineData.Split(fieldSeparator.ToCharArray()).Length == fieldLength){
86                                     list.Add(lineData);
87                                 }
88                             }
89                             lineData = sr.ReadLine();
90                         }
91                     }
92                 }
93             }catch(Exception e){
94                 throw e;
95             }
96             return list;
97         }
98
99         private List<string> GetInvalidFields(string linedata)
100         {
101             string[] fields = new string[] { "date", "time", "s-ip", "cs-method", "cs-uri-stem", "cs-uri-query", "s-port", "cs-username", "c-ip", "cs(User-Agent)", "cs(Referer)", "sc-status", "sc-substatus", "sc-win32-status", "time-taken" };
102             string[] datas = linedata.Split(fieldSeparator.ToCharArray());
103
104             List<string> temp = new List<string>();
105             for(int i=0; i<datas.Length; i++) {
106                 if(datas.Length > 255 && i != 10) {
107                     temp.Add(string.Format("{0} {1}",fields, datas));
108                 }
109             }
110             return temp;
111         }
112
113         public string FormatEncoding(string lineData)
114         {
115             string t = "";
116             Regex reg = new Regex("[\u4e00-\u9fa5]+");
117             MatchCollection matches = reg.Matches(lineData);
118             for(int i = 0; i < matches.Count; i++ ) {
119                 t += string.Format("{0},",matches.Value);
120             }
121             return t.TrimEnd(',');
122         }
123     }
124 }
125   
  下面是调用:
  

日志文件转换

1 namespace Log2Sql
2 {
3     class Program
4     {
5         static void Main(string[] args)
6         {
7             string inputDirectory = @"g:\iislog\logs";
8             string outputFileName = @"g:\iislog\log.txt";
9             string errorFileName = @"g:\iislog\error.txt";
10             try{
11                 FormatLog log = new FormatLog(inputDirectory, outputFileName, errorFileName);
12                 log.Execute();
13                 //log.WriteInvalidFields();
14             }
15             catch(Exception e){
16                 Console.Write(e.Message);
17             }
18             Console.Write("success");
19             Console.Read();
20         }
21     }
22 }  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-98877-1-1.html 上篇帖子: IIS发布网站部分特殊配置--图片上传至别的服务器、iis配置 通配符应用程序映、iis配置 C#底层拦截.htm文件请求 下篇帖子: IIS权限设置参考
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表